PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] how to send two different messages to two different users phpmailer

 July 07, 2022     php, phpmailer     No comments   

Issue

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 = 'user@email.com'
  $adminemail = 'admin@email.com';

  require 'PHPMailerAutoload.php';
  $mail = new PHPMailer(true);
  $mail->isHTML();
  $mail->IsSMTP(); 
  $mail->setFrom('admin@mysite.com', '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.


Solution

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();


Answered By - jeroen
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing