dsajkdadsa14222 2014-01-21 06:14
浏览 52
已采纳

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

I'm trying to send two versions of the same email to two recipients using phpMailer

Depending on the email I need to send one version or another.

At the moment I'm able to send only the same version to both email address

<?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>";
}}?>
  • 写回答

2条回答 默认 最新

  • duanbipu1720 2014-01-21 07:10
    关注

    If you're looking to do all the setup once for minimal repetition, simply clone your $mail object in your foreach($add) loop:

    foreach ( $add as $address ) {
        $current = clone $mail;
    
        if ( $address == 'first@mail.com' ) {
    
            $current->AddAddress($address, "xxxxxxxxx");
            $current->Body = $body;
            $current->send();
    
       } elseif.... 
    

    This creates a separate clone of your $mail object for every loop, allowing you to add different email bodies, subjects, etc. without having to rewrite all connection settings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵