how to send two different messages to two different users phpmailer
Asked Answered
M

3

10

I am sending mails to two different persons, two different messages one for user and one for admin.

  $message1='hello user'      
  $message2='hello admin'
  $email = '[email protected]'
  $adminemail = '[email protected]';

  require 'PHPMailerAutoload.php';
  $mail = new PHPMailer(true);
  $mail->isHTML();
  $mail->IsSMTP(); 
  $mail->setFrom('[email protected]', 'admin site'); 
  $mail->AddAddress( $email);
  $mail->Subject  = $subject;
  $mail->Body     =$message1;
  $mail->Send();
  //message for admin 
  $mail->Body     =$message2;
  //$adminemail = $generalsettings[0]["admin_email"]; 

   $mail->AddAddress($adminemail);
   $mail->Send();

But as a user I am receiving the message twice.. How to send two different messages to two different users.

Munro answered 1/3, 2017 at 13:37 Comment(0)
Z
28

You need to clear the recipients list before you add the new address for the second message. If you don't do that, the first recipient will receive the second message as well:

...
$mail->Body     =$message1;
$mail->Send();

//message for admin 

// Remove previous recipients
$mail->ClearAllRecipients();
// alternative in this case (only addresses, no cc, bcc): 
// $mail->ClearAddresses();

$mail->Body     =$message2;
//$adminemail = $generalsettings[0]["admin_email"]; 

// Add the admin address
$mail->AddAddress($adminemail);
$mail->Send();
Zerlina answered 1/3, 2017 at 13:52 Comment(0)
M
7

You can initiate phpmailer class two times.

$message1='hello user'      
$message2='hello admin'
$email = '[email protected]'
$adminemail = '[email protected]';

require 'PHPMailerAutoload.php';

$mail = new PHPMailer(true);
$mail->isHTML();
$mail->IsSMTP(); 
$mail->setFrom('[email protected]', 'admin site'); 
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->Body = $message1;
$mail->Send();

$mail2 = new PHPMailer(true);
$mail2->isHTML();
$mail2->IsSMTP(); 
$mail2->setFrom('[email protected]', 'admin site'); 
$mail2->AddAddress($adminemail);
$mail2->Subject = $subject;
$mail2->Body = $message2;
$mail2->Send();

This should work too:

$message1='hello user'      
$message2='hello admin'
$email = '[email protected]'
$adminemail = '[email protected]';

require 'PHPMailerAutoload.php';

$mail = new PHPMailer(true);
$mail->isHTML();
$mail->IsSMTP(); 
$mail->setFrom('[email protected]', 'admin site'); 
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->Body = $message1;
$mail->Send();

$mail->ClearAddresses();

$mail->AddAddress($adminemail);
$mail->Body = $message2;
$mail->Send();
Matelda answered 1/3, 2017 at 13:48 Comment(1)
Hi, I tried your second option because it's less code but when I add the last $mail->send() the mail gets sent 3 times. But when I remove it, it's sent twice which should be the norm. Why is that?Anglice
N
0

$mail->ClearAllRecipients(); // It removes old emails, After sending old emails use this code and again use addaddress() and send other users

Nebuchadnezzar answered 1/10, 2022 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.