how to send two different messages to two different users phpmailer

我是研究僧i 提交于 2019-12-05 17:24:08

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

You can initiate phpmailer class two times.

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

$mail2 = new PHPMailer(true);
$mail2->isHTML();
$mail2->IsSMTP(); 
$mail2->setFrom('admin@mysite.com', 'admin site'); 
$mail2->AddAddress($adminemail);
$mail2->Subject = $subject;
$mail2->Body = $message2;
$mail2->Send();

This should work too:

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

$mail->ClearAddresses();

$mail->AddAddress($adminemail);
$mail->Body = $message2;
$mail->Send();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!