【问题标题】:PHPMailer allows mime boundary to be visiblePHPMailer 允许 mime 边界可见
【发布时间】:2019-02-24 04:32:03
【问题描述】:

我正在使用 PHPMailer 通过 smtp-relay.gmail.com 发送电子邮件 - see previous post 通过 G-Suite 创建帐户后,我的凭据正在被接受,但是当电子邮件发送时,我可以看到纯文本版本,以及 html 版本,以及其他一些字符:

    ------example.com----250cd4bbb8be52d828379181e485c269 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Order Received

...

 ------example.com----250cd4bbb8be52d828379181e485c269 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

后面是html版本,结尾如下:

------example.com----250cd4bbb8be52d828379181e485c269-- 

当我使用香草版本时,它工作得很好,没有看到纯文本或 mime 边界数据:

$send = mail($recpEmails, $subject, $htmlMessage, $headers);

变量 $htmlMessage 仍然保存与以前相同的信息,但现在 PHPMailer 正在通过以下行发送它:

$mail->Body = $htmlMessage;

我不会看到带有破折号的纯文本或其他这些行。为什么通过 PHPMailer > smtp-relay.gmail.com 发送会改变结果?

是不是因为我加了下面这行?

$mail->IsHTML(true);

是否是由于以下行,如果是,那么我应该将其设置为什么?

$mail->SMTPDebug = 3;

G-Suite> 电子邮件中是否有需要更改的高级设置?

这是上一篇文章的更新代码:

  $mail = new PHPMailer(true);

  $mail->isSMTP();
  $mail->SMTPDebug = 3; 
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = "tls";
  $mail->Host = "smtp-relay.gmail.com";
  $mail->Port = "587";
  $mail->Username = "info@example.com";
  $mail->Password = "somePassword";
  $mail->setFrom("info@example.com");
  $mail->isHTML(true);
  $mail->Subject = $subject;
  $mail->Body = $htmlMessage;
  $mail->addAddress($recpEmails);
  $mail->Send();

提前致谢

【问题讨论】:

  • $htmlMessage 中有什么内容?
  • @tkausl $htmlMessage,这里有太多要发的,但基本上这里是一个sn-p - $plain_text = "Order Received"; $plain_text .= "感谢您的订购 $user_id,\r\n"; $htmlMessage = "--$mime_boundary\n"; $htmlMessage .= "Content-Type: text/plain; charset=us-ascii\n"; $htmlMessage .= "内容传输编码:7bit\n"; $htmlMessage .= "$plain_text\n"; $htmlMessage .= "

     
    ";等等……

  • 所以你真的得到了你告诉 PHPMailer 发送的内容。您不应该自己构建消息,这是 PHPMailers 的工作。
  • @tkausl,是的,你是对的,我相信我刚刚在这篇文章中找到了答案 - stackoverflow.com/questions/22507176/… - 我不应该建立边界,因为我必须处理 PHP 的邮件。相反,我应该只做 $mail->AltBody = $textMessage;对于文本部分,PHPMailer 将为我创建边界。我将更改我的代码,然后进行测试。如果我成功了,那么我会发布答案

标签: smtp phpmailer


【解决方案1】:

原来答案在下面stackoverflow post

我以前通过 PHP 的邮件在纯文本和 html 版本之间建立 mime 边界,方法如下:

创建文本消息 - 这应该与您的 html 完全相同,以避免被标记为垃圾邮件:

$plain_text = ""; // same words that will appear in the html

开始创建 mime 边界:

# -=-=-=- MIME BOUNDARY
$mime_boundary = "----example.com----".md5(time());
# -=-=-=- MAIL HEADERS

创建主题:

$subject = "example.com\n";

创建标题,其中将包括您之前开始的 mime 边界。此外,在标题中添加纯文本消息 - 在第一个 mime 边界之间:

$headers = "From: example.com <cs@example.com>\n";
$headers .= "Reply-To: example.com <cs@example.com>\n";
$headers .= "BCC: example.com <info@example.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";

$htmlMessage = "--$mime_boundary\n";
$htmlMessage .= "Content-Type: text/plain; charset=us-ascii\n";
$htmlMessage .= "Content-Transfer-Encoding: 7bit\n";
$htmlMessage .= "$plain_text\n";

$htmlMessage .= "--$mime_boundary\n";
$htmlMessage .= "Content-Type: text/html; charset=UTF-8\n";
$htmlMessage .= "Content-Transfer-Encoding: 8bit\n\n";
$htmlMessage .= "<html>\n";
$htmlMessage .= "<body>\n";
$htmlMessage .= "" // has to be the same words as the plain text to avoid being marked as spam
$htmlMessage .= "</body>\n";
$htmlMessage .= "</html>\n";

完成 mime 边界:

# -=-=-=- FINAL BOUNDARY
$htmlMessage .= "--$mime_boundary--\n\n";
# -=-=-=- SEND MAIL

但是,使用 PHPMailer,所有这些代码都不再需要,只需使用以下两行代码:

  $mail->AltBody = $plain_text; 
  $mail->Body = $htmlMessage;

以下是我测试和使用过的:

  $mail = new PHPMailer(true);

  $mail->isSMTP();
  $mail->SMTPDebug = 3; 
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = "tls";
  $mail->Host = "smtp-relay.gmail.com";
  $mail->Port = "587";
  $mail->Username = "cs@example.com";
  $mail->Password = "somePassword";
  $mail->setFrom("cs@example.com");
  $mail->isHTML(true);
  $mail->Subject = $subject;
  $mail->AltBody = $plain_text; 
  $mail->Body = $htmlMessage;
  $mail->addAddress($recpEmails);
  $mail->Send();

差点忘了,但这是我添加密件抄送的方式:

  $mail->addBCC("info@example.com"); 

【讨论】:

    猜你喜欢
    • 2012-09-07
    • 2014-06-19
    • 2018-07-06
    • 2014-12-08
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多