2012-07-04 3 views
0

phpmailer를 사용하여 대량 메일을 보낼 때 한 가지 문제가 없습니다.다른 데이터와 함께 데이터베이스에서 가져온 이메일 배열로 이메일을 보내려면 어떻게해야합니까?

$result = mysql_query("select * from $to",$conn) or die("list 
      selected ($to) does not exist ".mysql_error()); 

while ($row = mysql_fetch_array($result)) 
{   
    $email[] = $row['email'];   
    $student[] = $row['name']; 
} 

foreach ($email as $val => $uemail) { 
    $email = $uemail; 
    $students= $student[$val] ; 

    require("class.phpmailer.php"); 
     $mail = new PHPMailer(true); 

    try { 
      $mail->AddReplyTo('[email protected]', 'My Name'); 
      $mail->AddAddress("$email", "$student"); 
      $mail->SetFrom('[email protected]', 'MyName'); 
      $mail->AddReplyTo('[email protected]', 'My nameg'); 
      $mail->Subject = "$sub"; 

      $mail->MsgHTML("Dear $student<br> $msg <br> 
      <img src=\"$path\"> <p> 

      $host_upper 
      ______________________________________________________ 
      THIS IS AN AUTOMATED RESPONSE. 
      ***DO NOT RESPOND TO THIS EMAIL**** 

      "); 
      $mail->AddAttachment("$path2");  // attachment 

      $mail->Send(); 
      echo "Message Sent OK to $email </p>\n"; 
    } catch (phpmailerException $e) { 
     echo $e->errorMessage(); //Pretty error messages from PHPMailer 
    } catch (Exception $e) { 
     echo $e->getMessage(); //Boring error messages from anything else! 
    } 
} 

어떤 도움, 제안을 이해할 수있을 것이다 :

여기 내 코드입니다.

답변

0

당신이 foreach는 내부 $students$student[$val]을 할당하지만 당신은 당신의 phpmailer 객체에 배열 $student을 지정 :

$mail->AddAddress("$email", "$student"); 

shouldn 그럴거야

$mail->AddAddress("$email", "$students"); 

이 아닌 다른 이메일을 보낼 때마다 새 메일러 객체를 인스턴스화하는 것은 좋지 않으므로 AddAddress과 같은 동적 변수를 고수하고 오버로드를 피하기 위해 다른 모든 외부를 유지해야합니다. AddAddress처럼 다음과 같이 변경하십시오.

require_once("class.phpmailer.php"); 
$result = mysql_query("select * from $to",$conn) or die("list selected ($to) does not exist ".mysql_error()); 

      while ($row = mysql_fetch_array($result)) 
    { 

     $email[] = $row['email']; 

     $student[] = $row['name']; 
    } 

$mail = new PHPMailer(true); 
try { 
      $mail->AddReplyTo('[email protected]', 'My Name'); 
      $mail->SetFrom('[email protected]', 'MyName'); 
      $mail->AddReplyTo('[email protected]', 'My nameg'); 
      $mail->Subject = "$sub"; 
      $mail->AddAttachment("$path2"); 
      foreach ($email as $val => $uemail) { 
       $email = $uemail; 
       $students= $student[$val] ; 

       $mail->AddAddress("$email", "$students"); 
       $mail->MsgHTML("Dear $student<br> $msg <br> 
       <img src=\"$path\"> <p> 

      $host_upper 
      ______________________________________________________ 
      THIS IS AN AUTOMATED RESPONSE. 
      ***DO NOT RESPOND TO THIS EMAIL**** 

      "); 
       $mail->Send(); 
       $mail->ClearAddresses(); 
      echo "Message Sent OK to $email </p>\n"; 
       } 
      } catch (phpmailerException $e) { 
     echo $e->errorMessage(); //Pretty error messages from PHPMailer 
      } catch (Exception $e) { 
      echo $e->getMessage(); //Boring error messages from anything else! 
      } 
      } 
+0

감사합니다. 완전한 – kplus

1

당신은 while 루프 내부 메일을 보낼 수 있습니다

<?php 
require_once("class.phpmailer.php"); 

$result = mysql_query('SELECT `email`, `student`, `data1` FROM `students` ORDER BY `email` ASC;'); 

while ($row = mysql_fetch_assoc($result)) 
{ 
    $current_mail = new PHPMailer(true); 

    $current_mail->AddReplyTo('[email protected]', 'My Name'); 
    // .... 
    $current_mail->Send(); 
    unset($current_mail); 
} 
?> 
+0

감사합니다. 그것은 지금 완벽 – kplus

관련 문제