如何向两个不同的用户phpmailer发送两个不同的消息

时间:2017-03-01 13:37:06

标签: php phpmailer

我发送邮件给两个不同的人,两个不同的消息,一个用户,一个用于管理员。

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

但作为用户,我收到了两次消息..如何向两个不同的用户发送两条不同的消息。

提前致谢

2 个答案:

答案 0 :(得分:9)

在为第二条消息添加新地址之前,您需要清除收件人列表。如果您不这样做,第一个收件人也会收到第二条消息:

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

答案 1 :(得分:4)

你可以两次启动phpmailer类。

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

这也应该有效:

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