PHPmailer - 多次发送电子邮件

时间:2014-03-11 04:16:37

标签: php mysql email sendmail phpmailer

我正在使用PHPmailer发送电子邮件。截至目前,我成功地将电子邮件发送到一个地址。现在,我想只需点击一下即可发送多封电子邮件。

问题:我曾尝试使用下面的一些循环发送多封电子邮件,但我的输出错误了。是的,它发送电子邮件但只发送一个地址,电子邮件地址正在收到应该通过电子邮件发送到其他电子邮件的所有电子邮件。

例如,当我发送17封电子邮件时,这17封电子邮件只发送到一个地址。 电子邮件应根据数据库中的地址发送,并附带相应的唯一附件。示例:abc@gmail.com应附加abc.pdf,123 @ gmail.com应附加123.pdf。

我认为它在循环中。请帮我搞清楚。感谢。

require_once('phpmailer/class.phpmailer.php');
include("phpmailer/class.smtp.php"); 

$mail             = new PHPMailer();

$body             = file_get_contents('phpmailer/body.html');
$body             = preg_replace('/\/b]/','',$body);

$file ='phpmailer/mailpass.txt';
    if($handle = fopen($file,"r")){
        $contentpass = fread($handle,'15');
        fclose($handle);
        }

$mail->IsSMTP(); 
$mail->Host       = "smtp.gmail.com"; 
$mail->SMTPDebug  = 1;                   

$mail->SMTPAuth   = true;                  
$mail->SMTPSecure = "tls";                 
$mail->Host       = "smtp.gmail.com";      
$mail->Port       = 587;                   
$mail->Username   = "email@gmail.com";  
$mail->Password   = $contentpass;           

$mail->SetFrom("email@gmail.com", "Subject");

$mail->AddReplyTo("email@gmail.com","Subject");

$mail->Subject    = "Subjects";

$mail->AltBody    = "Subject";

$mail->MsgHTML($body);


$file='current_schoolyear.txt';
    if($handle = fopen($file,"r"))
    {
        $content = fread($handle,'9');
            fclose($handle);
    }



$input = addslashes($_POST['depchair']);                        


$email = "select email_address  from sa_student where schoolyear = '$input'"; 



if ($p_address=mysql_query($email))
{ 



  while($row = mysql_fetch_assoc($p_address))
  {



    $mail->AddAddress($row['email_address']);

    $input = addslashes($_POST['depchair']);                                                                                    

    $control = "select control_no  from sa_student where schoolyear = '$input'";

    if($ctrl=mysql_query($control)){

        $ctrl_no = mysql_result($ctrl, 0);


        $mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");  


    }
    else
    {

        echo "No attached document.";

    }

            if(!$mail->Send()) {
                    $message = "<div class=\"nNote nFailure\" >
                                    <p>Error sending email. " . $mail->ErrorInfo ."</p>
                                </div>";

            } else { 
                    $message = "<div class=\"nNote nSuccess\" >
                                    <p> Email have been sent to the examinees in ".$input_depchair. "! </p>
                                </div>";                            

                        }



       }

    }



else
{
    echo (mysql_error ());
}

更新后的代码:运行下面的代码后,我能够发送一封电子邮件并附上正确的附件。但是,只发送了一封电子邮件(数据库中的最后一个电子邮件地址),其余电子邮件未发送。

$input = addslashes($_POST['depchair']);                        


$email = "select email_address, control_no  from sa_student where schoolyear = '$input'"; 



if ($p_address=mysql_query($email))
{ 



  while($row = mysql_fetch_assoc($p_address))
  {

    $cloned = clone $mail;

    $cloned->AddAddress($row['email_address']);




        $cloned->AddAttachment("fpdf/pdf_reports/document/".$row['control_no'].".pdf");  




            if(!$cloned->Send()) {
                    $message = "<div class=\"nNote nFailure\" >
                                    <p>Error sending email. " . $mail->ErrorInfo ."</p>
                                </div>";

            } else { 
                    $message = "<div class=\"nNote nSuccess\" >
                                    <p> Email have been sent to the examinees in ".$input_depchair. "! </p>
                                </div>";                            

                        }
unset( $cloned );


       }

    }



else
{
    echo (mysql_error ());
}

2 个答案:

答案 0 :(得分:4)

发送电子邮件$mail->Send()后,请执行以下操作:

$mail->ClearAllRecipients();
在你的while循环中


所以你的基本while循环结构如下所示:

while($row = mysql_fetch_assoc($p_address)){

    $mail->AddAddress($row['email_address']);
    $mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");
    $mail->send();
    $mail->ClearAllRecipients(); 
    $mail->ClearAttachments();   //Remove all attachements

}

答案 1 :(得分:2)

在循环中,在添加收件人和附件之前创建$mail对象的克隆 - 然后使用克隆发送电子邮件。下一个循环迭代将创建一个没有先前地址和附件的新克隆:

while($row = mysql_fetch_assoc($p_address)) {

    $cloned = clone $mail;

    $cloned->AddAddress($row['email_address']);

    // add attchment to $cloned, etc.

    if ( $cloned->send() ) { /* etc */ }

    unset( $cloned );

}

这将“清除”您的每次迭代更改(如地址,附件等),而无需重新输入配置属性(如,来自,主机等)

<强>附录:

您的附件可能会完全相同,因为您没有为这些行获取新结果(在您的循环中):

$input=addslashes($_POST['depchair']);

$control = "select control_no  from sa_student where schoolyear = '$input'";

if ($ctrl=mysql_query($control)) {

    $ctrl_no = mysql_result($ctrl, 0);

    $mail->AddAttachment("fpdf/pdf_reports/document/".$ctrl_no.".pdf");

}

$ctrl_no将始终返回相同的结果,因为(我假设)$_POST['depchair']不会更改 - 因此$input$control$ctrl$ctrl_no对于每个循环都保持(有效)相同。你需要找到你实际想要成为每个循环的$ctrl_no的任何东西 - 现在你一遍又一遍地使用同一个循环。

以下查询可能有所帮助:

// replace
// $email = "select email_address  from sa_student where schoolyear = '$input'";

// with:
$students_query = "select email_address, control_no from sa_student where schoolyear = '$input'";

// then
// if ($p_address=mysql_query($email)) {
// while($row = mysql_fetch_assoc($p_address)) {

// becomes
if ( $students=mysql_query($students_query) ) {
while ( $row = mysql_fetch_assoc( $students ) ) {

// so that finally, etc
$cloned->AddAddress($row['email_address']);
$ctrl_no = $row['control_no'];

这会在同一查询中提取学生电子邮件地址及其control_no,确保它们通过循环保持相互关联。然后,您可以摆脱第二个中间循环查询,因为您需要的所有结果都是在第一个循环外查询中提取的。以上并不是您需要更改的所有代码,只是关键部分。