在Php中使用Foreach循环时邮件正文复制
我正在尝试向具有相同电子邮件正文但具有不同字符串变量的多个用户发送电子邮件。我的代码如下:在Php中使用Foreach循环时邮件正文复制,php,email,for-loop,foreach,Php,Email,For Loop,Foreach,我正在尝试向具有相同电子邮件正文但具有不同字符串变量的多个用户发送电子邮件。我的代码如下: $sql = "Select Trainee_Name,Session_ID FROM Session_Trainee WHERE Session_id='".$statement."'"; $fetched=sqlsrv_query($conn,$sql) ; if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));} whi
$sql = "Select Trainee_Name,Session_ID FROM Session_Trainee WHERE Session_id='".$statement."'";
$fetched=sqlsrv_query($conn,$sql) ;
if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));}
while($sno=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC))
{
$Trainee_Name[]=$sno['Trainee_Name'];
$Session_ID=$sno['Session_ID'];
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "xxxxx";
$mail->SetFrom('xxxxx');
$mail->Subject = "xxxxx"";
foreach($Trainee_Name as $traineename)
{
//$email_body = $email_body ."Dear ". $Trainee_Name .",<br/><br/>";
$email_body = $email_body ."A <strong> Session Evaluation Form</strong> form has been sent for Evaluation to you <br/>";
$email_body = $email_body ."<Strong><p><a href='http://xxxxx/Evaluation/form3.php?Session_id=$statement';> Click here to proceed with the evaluation </a>Immediate Evaluation Form</p></strong>";
$email_body = $email_body ."<Strong><p><a href='http://xxxxx/Evaluation/pre_evaluation.php?Session_id=$statement';> Click here to proceed with the evaluation </a>Pre Evaluation Form</p></strong>";
$email_body = $email_body ."<Strong><p><a href='http://xxxxx/Evaluation/post_evaluation.php?Session_id=$statement';> Click here to proceed with the evaluation </a>Post Evaluation Form</p></strong>";
$mail->MsgHTML($email_body);
$username="xxxxx";
$password="xxxxx";
$lc = ldap_connect("xxxxx") or
die("Couldn't conn/ect to AD!");
ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_bind($lc,$username,$password);
$base = "OU=xxxxx,DC=xxxxx,DC=xxxxx";
$filt = "(&(&(&(objectCategory=person)(objectClass=user)(name=$traineename*))))";
$sr = @ldap_search($lc, $base, $filt);
$info = ldap_get_entries($lc, $sr);
for ($j = 0; $j < $info["count"]; $j++)
{
$add = $info[$j]['mail'][0];
$address[] = $add;
echo $add."<br/>";
///$mail->AddAddress($add);
$mail->AddAddress('xxxxx');
}
if ($j == 0)
{
echo "No matches found!<br/>";
}
ldap_close($lc);
}
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
} else {
echo "Session Details Sent to the Trainees";
}
}
邮件被发送给用户,但正文在每封邮件中都是重复的。也就是说,如果有3个学员姓名,则在每封邮件中重复三次。下面是一封邮件的截图:
我哪里出错了?您必须在每个循环上启动email\u body变量:
foreach($Trainee_Name as $traineename)
{
//$email_body = $email_body ."Dear ". $Trainee_Name .",<br/><br/>";
$email_body = "A <strong> Session Evaluation Form</strong> form has been sent for Evaluation to you <br/>";
//rest of the loop code...
}
在当前代码中,您在每个循环中连接此变量,而不是从空循环开始。已经完成了更改,但仍然获得了重复项。奇怪的是,它不应该。。。第一行是不是亲爱的。。。。你的循环中的一部分确实在你的实际代码中被注释了?在线测试了更正,我觉得不错:相比之下,你的初始代码:@NjiruMwaniki因为这里没有其他地方有email_body,你可能在其他地方有问题,而不是这个代码部分。你的实际代码中是否存在语法错误?您在这行有一个额外的双引号,如下所示:$mail->Subject=xxxxx;