【发布时间】: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。