我有一个PHP foreach循环,它通过电子邮件提示运行并发送电子邮件。 我的问题是,它会从提示中的每一行发送一封电子邮件给提示中的每一行,所以假设提示中有50封邮件,我会收到50封电子邮件。不酷啊
foreach就是这样:
$cue = new NewsletterHandler;
$cue->GetEmailCue($letter_id, $lhash);
require_once(INCLUDE_DIR."/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "tegl@xxxxxxx";
$mail->FromName = "Randers Tegl";
$mail->Host = "smtp.xxxxx.dk";
$mail->Mailer = "smtp";
foreach($cue->email_row as $key => $value) {
$mail->AddAddress($value);
$mail->Subject = $subject ." - ". date("d-m-Y");
$mail->Body = $this->htmlTemplate;
if(!$mail->Send()) {
echo "E-mailen er ikke sendt til ". $value;
echo "Mailer Error: ". $mail->ErrorInfo;
echo "<br />";
mail("morten@domain.dk", "Error", "Letter not send. ". $mail->ErrorInfo ."");
} else {
$sql_update = "UPDATE newsletter_emailcue SET time_recieved = NOW() WHERE email = '". $value ."'";
SQLHandling::SQLquery($sql_update);
echo "Newsletter send to ". $value ."<br />";
}
}
GetEmailCue函数:
function GetEmailCue($letter_id, $lhash) {
$sql = "SELECT * FROM newsletter_emailcue WHERE mail_to_recieve = '". $letter_id ."' AND time_recieved = '0000-00-00 00:00:00' LIMIT 50";
$result = SQLHandling::SQLquery($sql);
if(mysql_num_rows($result) < 1) {
Main::txtOutput("Der er ikke nogle modtagere til nyhedsbrevet", "TXT_ERR");
/*** If there aren't any emails in the cue table, but a pending letter was found
* that letter will be updated with '2' in status, which is send completed.
*/
$sql_update = "UPDATE newsletter_items SET status = 2 WHERE letter_id = '". $letter_id ."'";
SQLHandling::SQLquery($sql_update);
} else {
while($row = mysql_fetch_array($result)) {
$this->email_row[] = $row["email"];
}
}
return $this->email_row;
}
如果我var_dump($cue->email_row) outside the foreach
我得到array(2) { [0]=> string(17) "morten@domain1.dk" [1]=> string(14) "morten@domain2.dk" }
但我得到了
array(2) { [0]=> string(17) "morten@adomain1.dk" [1]=> string(14) "morten@domain2.dk" }
array(2) { [0]=> string(17) "morten@domain1.dk" [1]=> string(14) "morten@domain2.dk" }
答案 0 :(得分:1)
我找到了!我忘了把
$mail->ClearAddresses();
$mail->ClearAttachments();
在脚本的末尾,因此它在通过$mail->AddAddress($value);
答案 1 :(得分:0)
一些事情: