use*_*474 4 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 ());
}
Run Code Online (Sandbox Code Playgroud)
更新后的代码:运行下面的代码后,我能够发送一封带有正确附件的电子邮件。但是,只发送了一封电子邮件(数据库中的最后一个电子邮件地址),其余的电子邮件都没有发送。
$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 ());
}
Run Code Online (Sandbox Code Playgroud)
发送电子邮件后$mail->Send()
,执行以下操作:
$mail->ClearAllRecipients();
Run Code Online (Sandbox Code Playgroud)
在你的 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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7419 次 |
最近记录: |