2

I am trying to send emails to multiple users with the same email body but with different string variables. My code is as below :

$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";
        }
}

The mails are being sent to the users, but the body is duplicated in each mail. I.e. if there are 3 trainee names, the body is duplicated thrice in each mail. Below is a screenshot of a mail :

enter image description here

Where have I gone wrong?

1
  • Is the syntax error present here in your actual code? You have an extra double quote on the line that is like this: $mail->Subject = "xxxxx""; <-- You have two closing quotes. Commented May 20, 2016 at 16:14

1 Answer 1

3

You have to initiate the email_body variable on each loop:

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...
}

In your current code, you're concatenating this variable in each loop, without starting from an empty one.

Sign up to request clarification or add additional context in comments.

4 Comments

Have done the changes but still getting the duplicates.
Strange, it shouldn't... Does the first line "Dear...." of your loop is indeed commented in your actual code?
Tested the correction online, looks good to me: sandbox.onlinephpfunctions.com/code/… In comparison, your initial code: sandbox.onlinephpfunctions.com/code/…
@NjiruMwaniki Since there is no other place with email_body here, you might have another problem elsewhere than this code portion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.