在Php中使用Foreach循环时邮件正文复制

时间:2016-05-20 13:55:47

标签: 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 ));}
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("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
        } else {
            echo "Session Details Sent to the Trainees";
        }
}

邮件正在发送给用户,但每封邮件中的邮件都是重复的。即如果有3个受训者姓名,则每封邮件中的正文重复三次。以下是邮件的屏幕截图:

enter image description here

我哪里出错?

1 个答案:

答案 0 :(得分:3)

您必须在每个循环上启动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... } 变量:

 EmployeeEdit wEdit = new EmployeeEdit(ref selectedEmployee);
 wEdit.Closing += (s, e) => 
 {
      //place code to refresh your grid here
 }
 wEdit.Show();

在当前代码中,您将在每个循环中连接此变量,而不是从空循环开始。