我有一个 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" }