smtp - PHPMailer 允许 mime 边界可见
问题描述
我正在使用 PHPMailer 通过 smtp-relay.gmail.com 发送电子邮件 -请参阅上一篇文章 通过 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();
提前致谢
解决方案
原来答案在下面的stackoverflow帖子中
我曾经通过 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");
推荐阅读
- verification - 一个明显的不变量在 Dafny 中失败了
- r - 有没有办法仅在 R Markdown 中导出为 PDF 时添加换行符?
- css - 为什么 font-family 不能在我网站上的所有页面上工作?
- angular - 如何在 Angular 服务测试中刷新 requestIdleCallback?
- python - PyQt5 动态删除 QListWidgets 和 QPushButton 并与 listItems 交互
- php - PHP、Mysql 通知服务器要求
- php - 如何解码此 Json 文件并使用 PHP 访问 Country 值
- firefox - 如何告诉 Firefox 信任“xxx.localhost”作为安全来源?
- java - mac无法导入maven项目
- html - Angular 8/9:如何使用插值设置 data-parent 属性?