我正在尝试准备电子邮件html,我有两组客户 - 1组只会收到电子邮件 2组将收到相同的电子邮件+凭证内.. 我准备了一个简短的代码来说明问题。这是:
$voucher='<div>VOUCHER IMAGE COMES HERE</div>';//only group for voucher must see it
$mail_description='Here comes my mail text';
$voucherarray=array("mail1@gmail.com","mail3@gmail.com");
$mailaray=array("mail1@gmail.com","mail2@gmail.com");
$html='';
foreach($mailaray as $email){
$html.='<div>'.$mail_description.'</div>';
if(in_array($email, $voucherarray)){
$html.=$voucher;
echo $email.'is for voucher';
}else{ $email.'is not for voucher';}
$html.='<table><tr><td>here is some other text</td></tr></table>
<div clss="footer"></div>';
mail($email,'subject',$html);
}
echo $html.'<br />';
此代码打印出来:
mail1@gmail.comis for voucher
Here comes my mail text
VOUCHER IMAGE COMES HERE
Here comes my mail text
here is some other text
Here comes my mail text
VOUCHER IMAGE COMES HERE
Here comes my mail text
here is some other text
它有什么问题以及为什么它打印出这个邮件的凭证图像来源,因为只有一张是凭证? 它还向所有组用户发送相同的邮件
答案 0 :(得分:2)
您需要将您在邮件中发送的html和要为发送邮件的作业看到的输出分开。您需要在每次迭代后清空邮件正文变量,您不希望每个客户端都接收先前客户端的邮件内容。
$voucher='<div>VOUCHER IMAGE COMES HERE</div>';//only group for voucher must see it
$mail_description='Here comes my mail text';
$voucherarray=array("mail1@gmail.com","mail3@gmail.com");
$mailaray=array("mail1@gmail.com","mail2@gmail.com");
$html='';
foreach($mailaray as $email){
$mailHtml = '';
$mailHtml.='<div>'.$mail_description.'</div>';
if(in_array($email, $voucherarray)){
$mailHtml.=$voucher;
echo $email.'is for voucher<br>';
}else{
echo $email.'is not for voucher<br>';
}
$mailHtml.='<table><tr><td>here is some other text</td></tr></table><div clss="footer"></div>';
mail($email,'subject',$mailHtml);
$html .= '<br>' . $mailHtml;
}
echo $html.'<br />';