重用 PHPMailer 发送第二封电子邮件?

问题描述 投票:0回答:2

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

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

目前我有这个:

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 组件来发送另一封电子邮件,而无需定义服务器详细信息?我

php phpmailer
2个回答
9
投票

试试这个:

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/>"; }

0
投票

将 Mailer 创建提取到一个函数中,它看起来会更好

use PHPMailer\PHPMailer\PHPMailer;

function createMailer(): PHPMailer
{
    $mailer = new PHPMailer();

    $mailer->isSMTP();
    $mailer->Host = getenv('mail.host');
    $mailer->SMTPAuth = true;
    $mailer->Username = getenv('mail.username');
    $mailer->Password = getenv('mail.password');
    $mailer->SMTPSecure = 'tls';
    $mailer->Port = 587;
    $mailer->setFrom(getenv('mail.from.address'), getenv('mail.from.name'));
    $mailer->isHTML();
    $mailer->CharSet = PHPMailer::CHARSET_UTF8;

    return $mailer;
}
  • 我正在从环境变量中读取一些值 - 不过你可以对字符串进行硬编码

使用方法

$mailer = createMailer();

// 1st mail
$mailer->clearAddresses();
$mailer->addAddress('[email protected]');
$mailer->Subject = 'Your Subject';
$mailer->Body = 'Hello name,<br><br>your order has been received.';
if ($mailer->send()) {
    echo '1st mail sent successfully.';
} else {
    echo 'Failed to send 1st mail.';
}

// 2nd mail
$mailer->clearAddresses();
$mailer->addAddress('[email protected]');
$mailer->Subject = 'Your Subject';
$mailer->Body = 'Hello name,<br><br>your order has been received.';
if ($mailer->send()) {
    echo '2nd mail sent successfully.';
} else {
    echo 'Failed to send 2nd mail.';
}
  • 在发送邮件之前,我们总是先清除收件人
    • 如果您使用带有
      ->addCC
      ->addBCC
      的抄送或密送字段,则必须使用
      ->ClearAllRecipients()
      而不是
      ->clearAddresses()
    • 如果您使用
      ->addAttachment()
      发送附件,则必须致电
      ->clearAttachments()
      来清除它们

来源

© www.soinside.com 2019 - 2024. All rights reserved.