gpt4 book ai didi

PHPmailer - 多次发送电子邮件

转载 作者:可可西里 更新时间:2023-11-01 07:27:30 25 4
gpt4 key购买 nike

我正在使用 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 ());
}

最佳答案

发送电子邮件$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

}

关于PHPmailer - 多次发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22316358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com