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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算