【问题标题】:Reusing PHPMailer to send out a second email?重用 PHPMailer 发送第二封电子邮件?
【发布时间】:2015-03-21 15:12:58
【问题描述】:

我正在开发一个订购系统,该系统需要向客户、分销商和网站所有者发送电子邮件。

所有者和分销商可以收到相同的电子邮件(订单详细信息等),但客户需要一封正文稍有不同的电子邮件,以包含感谢信息。

目前我有这个:

require_once('PHPMailer/class.phpmailer.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "";
$mail->Port = 587; // or 587
//$mail->IsHTML(true);
$mail->Username = "";
$mail->Password = "";
$mail->SetFrom("");
$mail->IsHTML(true);
$mail->Subject = "Online Shop - New Order - " . $orderNumber;
$mail->Body = "A new order has been placed with Online Shop. Please find the delivery details below: <br><br> " . $orderDetails;
$mail->AddAddress($distributerEmail); //Distributor email address
$mail->AddAddress($ownerEmail); //Owner email address
if($mail->Send()){  }else{ $error = "Error sending message! <br/>"; }

如何重用 $mail 组件来发送另一封电子邮件,而无需同时定义服务器详细信息?我

【问题讨论】:

  • 最好的办法是把它变成一个函数吗?
  • 接受的答案是好的,尽管看起来您已经使用了一个旧示例来作为您的代码的基础(并且可能正在运行旧版本的 PHPMailer)。您所做的与 PHPMailer 捆绑的 the mailing list example 非常相似。
  • @Synchro 我正在使用此处提供的示例代码:phpmailer.worxware.com/?pg=tutorial
  • @Synchro 使用 5.2.7
  • 是的,这是该网站的过时年数,而 5.2.7 是从 2013 年 9 月开始的 - 从那时起事情就发生了变化。查看on GitHub,阅读文档和示例文件夹中的自述文件和其他文档以及github wiki。

标签: php phpmailer


【解决方案1】:

试试这个:

require_once 'PHPMailerAutoload.php';
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "";
$mail->Port = 587; // or 587
//$mail->IsHTML(true);
$mail->Username = "";
$mail->Password = "";
$mail->SetFrom("");
$mail->IsHTML(true);
$mail->Subject = "Online Shop - New Order - " . $orderNumber;
$mail->Body = "A new order has been placed with Online Shop. Please find the delivery details below: <br><br> " . $orderDetails;
$mail->AddAddress($distributerEmail); //Distributor email address
$mail->AddAddress($ownerEmail); //Owner email address
if($mail->Send()){  }else{ $error = "Error sending message! <br/>"; }

// We delete the addresses of distributer and owner.
$mail->ClearAddresses();

$mail->AddAddress($customerEmail);
$mail->Body = "new body";

if($mail->Send()){  }else{ $error = "Error sending message! <br/>"; }

【讨论】:

  • 我发现了这个:$mail-&gt;ClearAllRecipients(); 是一样的吗?
  • 在你的情况下是的。 ClearAllRecipientes(); 清除 TOBCCCCClearAddresses() 只清除 TO
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 2017-01-16
  • 2017-06-05
  • 2019-04-16
相关资源
最近更新 更多