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条)

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据