Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send two different messages to two different users phpmailer

Tags:

php

phpmailer

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.

like image 866
scriptkiddie1 Avatar asked Mar 01 '17 13:03

scriptkiddie1


People also ask

How can I send multiple emails in PHPMailer?

// First Email $to = array( '[email protected]', '[email protected]',); $subject = "Subject"; $message = $message_start. $message_ONE. $message_end; sendMail(); // Second Email $to = array( '[email protected]', '[email protected]',); $subject = "Subject"; $message = $message_start.

Does PHPMailer have a limit?

PHPMailer does not set any limits, nor does the mail() function, it's only ISPs like GoDaddy that do. However, they do so by blocking access to normal SMTP ports, and/or redirecting SMTP connections to their own servers ( *.

Why it is advantages to use PHPMailer for sending and receiving email?

PHPMailer can use a non-local mail server (SMTP) if you have authentication. Further advantages include: It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS.

Is PHPMailer secure?

PHPMailer doesn't create/use any SQL itself, nor does it have anything to do with javascript, so it's secure on those fronts.


1 Answers

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();
like image 115
jeroen Avatar answered Sep 30 '22 05:09

jeroen