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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了