使用class.phpmailer.php将同一电子邮件的两个版本发送给两个不同的收件人

使用class.phpmailer.php将同一电子邮件的两个版本发送给两个不同的收件人,php,email,loops,phpmailer,Php,Email,Loops,Phpmailer,我正在尝试使用phpMailer将同一电子邮件的两个版本发送给两个收件人 根据电子邮件,我需要发送一个或另一个版本 目前,我只能向这两个电子邮件地址发送相同的版本 <?php require_once('class.phpmailer.php'); if(isset($_POST['Submit'])) { $fname = $_POST['fname']; $maileremail = $_POST['maileremail']; $subject = $_

我正在尝试使用phpMailer将同一电子邮件的两个版本发送给两个收件人

根据电子邮件,我需要发送一个或另一个版本

目前,我只能向这两个电子邮件地址发送相同的版本

<?php               
require_once('class.phpmailer.php');
if(isset($_POST['Submit'])) {
$fname = $_POST['fname'];
$maileremail = $_POST['maileremail'];
$subject = $_POST['subject']; 
$message = $_POST['message']; 

$mail = new PHPMailer();

$text = 'The person that contacted you is  '.$fname.'  E-mail: '.$maileremail.' Subject: '.$subject.' Message: '.$message.' :';
$body = eregi_replace("[\]",'',$text);

$text2 = 'The person that contacted you is  '.$fname.'  E-mail: REMOVED - To get email address you must login Subject: '.$subject.' Message: '.$message.' :';
$body2 = eregi_replace("[\]",'',$text2);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxxxx.xxxxx.xxx"; // SMTP server
$mail->SMTPAuth = true;                  // enable SMTP authentication
$mail->SMTPKeepAlive = true;                  // SMTP connection will not close after each email sent
$mail->Host = "xxxxx.xxxxx.xxx"; // sets the SMTP server
$mail->Port = XXXXXXXX;                    // set the SMTP port for the GMAIL server
$mail->Username = "xxxxx@xxxxx.xxx"; // SMTP account username
$mail->Password = "XXXXXXXX";        // SMTP account password
$mail->SetFrom('xxxxx@xxxxx.xxx', 'XXXXXXXX');
$mail->Subject = $subject;

$add = array("first@email.xxx", "second@email.xxx");

foreach($add as $address) { 
    if($address == "first@email.xxx") {
        $mail->AddAddress($address, "xxxxxxxxx"); 
        $mail->Body = $body;
    }
    elseif($address == "second@email.xxx") {
        $mail->AddAddress($address, "xxxxxxxxx"); 
        $mail->Body = $body2;
    }                       
}

if(!$mail->Send()) {
     echo "Mailer Error: " . $mail->ErrorInfo;
} else {
     echo "<h3><b>Message sent!</b></h3>";
}}?>

我想您应该搜索
$maileremail
中的
$add
并发送所需的电子邮件

$add = array("first@email.xxx", "second@email.xxx");
$mail->AddAddress($maileremail, "xxxxxxxxx");

if($maileremail === $add[0])
$mail->Body = $body;
if($maileremail === $add[1])
$mail->Body = $body2;


if(!$mail->Send()) {
     echo "Mailer Error: " . $mail->ErrorInfo;
} else {
     echo "<h3><b>Message sent!</b></h3>";
}
$add=array(“first@email.xxx", "second@email.xxx");
$mail->AddAddress($maileremail,“xxxxxxxxx”);
如果($maileremail===$add[0])
$mail->Body=$Body;
如果($maileremail==$add[1])
$mail->Body=$body2;
如果(!$mail->Send()){
回显“邮件错误:”.$mail->ErrorInfo;
}否则{
回显“已发送消息!”;
}

编辑:
我误解了这个问题。是正确的。

如果您希望一次完成所有设置以减少重复,只需在
foreach($add)
循环中克隆
$mail
对象:

foreach ( $add as $address ) {
    $current = clone $mail;

    if ( $address == 'first@mail.com' ) {

        $current->AddAddress($address, "xxxxxxxxx");
        $current->Body = $body;
        $current->send();

   } elseif.... 

这将为每个循环创建一个单独的
$mail
对象克隆,允许您添加不同的电子邮件正文、主题等,而无需重写所有连接设置

你能举一个if/else if条件的例子吗?使用$mail->ClearAllRecipients();只向一个收件人发送电子邮件。谢谢我假设你有一个收件人列表,你希望不同的收件人有不同的电子邮件正文,而不是你希望向每个人发送电子邮件?是的,这就是为什么我有$body和$body2,这取决于我想发送一个“正文”或另一个“正文”的电子邮件地址。很抱歉,$maileremail=$_POST['maileremail'];所以$maileremail!=$添加[0]和$maileremail!=$添加[1]您在$add中有一个电子邮件地址列表,您需要将$maileremail与其中一个地址进行比较,并在此基础上设置邮件正文,对吗?这就是代码所做的。布赖恩·诺特:它工作正常,但我得到以下错误“邮件错误:您必须提供至少一个收件人电子邮件地址”。但正如我所说,它工作正常。