Reputation: 565
I have created a code, where mails are sent to different people with different bodies, but only if some conditions are satisfied. My code is as follows :
set_time_limit(300);
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "*********";
$mail->SetFrom('*********');
$mail->Subject = "System Change";
$add = array("a", "b");
foreach ($add as $address) {
$mail->clearAttachments();
$mail->clearAllRecipients();
if ($Credit == 'N' && $CMU == 'N') {
if ($Ops_Approval == 'Approved' && $Risk_Approval == 'Approved' && $Finance_Approval == 'Approved') {
if ($SeniorManagement == 'Y') {
if ($address == "a") {
$mail->AddAddress('********');
$mail->MsgHTML("Mail1");
$mail->send();
if ($mail->Send()) {
echo "Mail 1";
} else echo "ERROR IN SENDING MAILS";
exit;
}
if ($address == "b") {
$mail->AddAddress('****');
$mail->MsgHTML("Mail2");
$mail->send();
if ($mail->Send()) {
echo "Mail 2";
} else echo "ERROR IN SENDING MAILS";
exit;
}
}
}
}
}
What is working - Mails are being sent. What is not working - Both the mails have the same body i.e Mail1 . I have tried creating different if loops, yet get the same result. Appreciate any suggestions :)
Upvotes: 1
Views: 108
Reputation: 1045
You are calling Send()
twice by using it again in your if. You can remove the line above if($mail->send()){
. Also don't forget to put the code after the else inside brackets, otherwise it won't execute the exit.
Upvotes: 1
Reputation: 11808
We don't think 2 mails are sent because you have written exit on if and not on else part. Try this:-
if ( $address == "a" ) {
$mail->AddAddress('********');
$mail->MsgHTML("Mail1");
//$mail->send();
if($mail->Send()) {
echo "Mail 1";
} else {
echo "ERROR IN SENDING MAILS";
exit;
}
}
if ( $address == "b" ) {
$mail->AddAddress('****');
$mail->MsgHTML("Mail2");
//$mail->send();
if($mail->Send()) {
echo "Mail 2";
} else {
echo "ERROR IN SENDING MAILS";
exit;
}
}
Upvotes: 2