PHPmailer - 多次发送电子邮件 [英] PHPmailer - Multiple sending of e-mail

查看:191
本文介绍了PHPmailer - 多次发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPmail发送电子邮件。到目前为止,我成功地发送电子邮件到一个地址。现在,我想通过一键点击发送多个电子邮件。



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



例如,当我发送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,主题);

$ mail-> AddReplyTo(email@gmail.com,主题);

$ mail-> Subject =Subjects;

$ mail-> AltBody =主题;

$ mail-> MsgHTML($ body);


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



$ input = addslashes($ _ POST ['depchair']);


$ email =从sa_student选择email_address其中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);


}
其他
{

回显无附件。

}

如果(!$ mail-> Send()){
$ message =< div class = \nNote nFailure\ >
< p>发送电子邮件时出错。 $ mail-> ErrorInfo。< / p>
< / div>;

} else {
$ message =< div class = \nNote nSuccess\>
< p>电子邮件已发送给。$ 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))
{

$克隆=克隆$ mail;

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




$克隆 - > AddAttachment(fpdf / pdf_reports / document /\".$ row ['control_no']。pdf);




如果(!$克隆 - > Send()){
$ message =< div class = \nNote nFailure\>
< p>发送电子邮件时出错。 $ mail-> ErrorInfo。< / p>
< / div>;

} else {
$ message =< div class = \nNote nSuccess\>
< p>电子邮件已发送给。$ input_depchair。 !< / p>
< / div>;

}
unset($克隆);


}

}



else
{
echo mysql_error());
}


解决方案

发送电子邮件 $ 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(); //删除所有附件

}


I am sending emails using PHPmailer. As of now, I am successful in sending email to one address. Now, I want to send multiple emails in just one click.

PROBLEM: I have tried to use some loops below to send multiple email but I get wrong outpout. Yes, it sends email but to only one address, and the email address is getting all of the emails that are supposed to be emailed to other emails.

For example, when I send 17 emails, those 17 emails are sent to only one address. The emails should be sent according to the addresses in the database, with corresponding unique attachments. Example: abc@gmail.com should have abc.pdf attached, and 123@gmail.com should have 123.pdf attached.

I think it's in the loop. Please help me figure it out. Thanks.

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 ());
}

UPDATED CODE: After running the code below, I was able to send an email and with the correct attachment. However, there was only ONE email sent (the last email address in the database), and the rest of the emails were not sent.

$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 ());
}

解决方案

After you send an email $mail->Send(), execute this:

$mail->ClearAllRecipients();

in your while loop.
So your basic while loop structure looks like this:

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

}

这篇关于PHPmailer - 多次发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆