如何向两个不同的用户发送两个不同的邮件phpmailer

如何向两个不同的用户发送两个不同的邮件phpmailer

问题描述:

我正在向两个不同的人发送邮件,两个不同的消息一个用于用户,一个用于管理员。如何向两个不同的用户发送两个不同的邮件phpmailer

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

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

在此先感谢

您需要在第二个消息添加新的地址之前清除收件人列表。如果你不这样做,第一收件人将收到第二个消息,以及:

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

谢谢老兄的回答... – scriptkiddie1

可以启动的PHPMailer类两倍。

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

这应该工作太:

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

谢谢老兄的回答是helpe我 – scriptkiddie1