如何向包含其他数据的数据库中提取的电子邮件数组发送电子邮件

时间:2012-07-04 09:58:49

标签: php mysql phpmailer

我一直在使用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('info@bratim.com', 'My Name');
           $mail->AddAddress("$email", "$student");
           $mail->SetFrom('info@me.com', 'MyName');
           $mail->AddReplyTo('info@me.com', '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!
    } 
}

任何帮助,建议将不胜感激。

2 个答案:

答案 0 :(得分: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('info@bratim.com', 'My Name');
    // ....
    $current_mail->Send();
    unset($current_mail);
}
?>

答案 1 :(得分:0)

您将$student[$val]分配给foreach中的$students但是您已分配 数组$student到你的phpmailer对象:

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

不应该是

 $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('info@bratim.com', 'My Name');
            $mail->SetFrom('info@me.com', 'MyName');
            $mail->AddReplyTo('info@me.com', '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!
             } 
            }