2016-05-20 1 views
2

同じメール本文ではなく、異なる文字列変数を持つ複数のユーザーに電子メールを送信しようとしています。私のコードは以下の通りです:Mail Body PHPで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人の研修生の名前があれば、各メールで3回重複しています。

enter image description here

私が間違っている:下記のメールのスクリーンショットはありますか?

+0

実際のコードに構文エラーがありますか? '$ mail-> Subject =" xxxxx ""; '< - 閉じ引用符が2つあります – Henders

答えて

3

あなたは、各ループ上のemail_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... 
} 

を。

+0

変更を加えたが、重複はまだ残っています –

+0

奇妙なことに、それは...すべきではないあなたのループの最初の行 "親愛なる...."実際にあなたの実際のコードでコメントされていますか? – Veve

+0

最初の行は、コード内のコメントです –

関連する問題