【问题标题】:how to send multipart/alternative email with PHP correctly如何使用 PHP 正确发送多部分/替代电子邮件
【发布时间】:2018-02-17 08:52:23
【问题描述】:

我正在尝试使用内置的 PHP 邮件功能来发送包含我的消息的 html 和纯文本版本的多部分消息。我一直在玩不同的编码类型,但是我一直遇到问题。最初我将Content-Transfer-Encoding 设置为Binary,但是这导致每78 个字符放置一个感叹号。我也试过base64,但我相信base64对于我正在做的事情来说太过分了。

我所做的只是发送基本的 HTML,而不是编码的图像、文件或附件。我更喜欢一种仍然允许人类可读源代码的编码方法。

我听说Quoted-Printable 是我正在寻找的东西,但是当我尝试使用该编码类型发送消息时,结果看起来真的很奇怪。我注意到消息源代码中散布着一堆" 符号。

这是我正在使用的代码:

    $to = "to@test.com";
    $subject = "test subject";
    $boundary = uniqid('np');               
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From: from-address@test.com\r\n";
    $headers .= "Reply-To: reply-address@test.com\r\n";
    $headers .= "Return-Path: return-path@test.com\r\n";
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
    $message = "This is a MIME encoded message.";

    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
    $message .= $plainTextMessage;

    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-Type: text/html; charset=UTF-8\r\n";
    $message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
    $message .= $HTMLmessage;

    $message .= "\r\n\r\n--" . $boundary . "--";

    $ok = mail($to,$subject,$message,$headers);

我到底做错了什么?

【问题讨论】:

  • 我建议使用库而不是尝试自己滚动这个库。电子邮件很快变得异常复杂。试试github.com/PHPMailer/PHPMailer 之类的东西。相信我,它会让你的生活更轻松。
  • @matt 我尝试了 PHPMailer,但无论我做什么,我都无法让它在我的服务器上工作。它由 GoDaddy 托管。连 GoDaddy 都说只使用内置的邮件功能。
  • 然后尝试与mailgun.com 集成。您进行 API 调用,他们会为您构建和发送电子邮件。

标签: php email transfer-encoding quoted-printable


【解决方案1】:

你好,试试下面的代码,

    $to = "to@test.com";
    $subject = "test subject";
    $plainTextMessage = "Hi all";
    $HTMLmessage = "<b>Hi all</b>";
     //$boundary = uniqid('np');               
    $boundary = md5(uniqid(time()));   
    $headers .= "From: from-address@test.com\r\n";
    $headers .= "Reply-To: reply-address@test.com\r\n";
    $headers .= "Return-Path: return-path@test.com\r\n";
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
    $message = "This is a MIME encoded message.";

    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
    $message .= $plainTextMessage;

    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-Type: text/html; charset=UTF-8\r\n";
    $message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
    $message .= $HTMLmessage;

    $message .= "\r\n\r\n--" . $boundary . "--";

    $ok = mail($to,$subject,$message,$headers);

希望对你有帮助

【讨论】:

  • 我已经确认这不是边界问题,这是您在此处提供的唯一建议。
  • 是的。今天只有我在做这个。这段代码对我来说很好用。你有什么错误吗?
【解决方案2】:

无需第三方库或外部邮件转发主机。这就是我使用的,它就像一个魅力。它还通过强制标头使用 $from 地址来修复一些潜在的安全漏洞,否则可能会泄露您的系统用户:

private function sendMail($to, $from, $fromName, $subject, $text, $html)
{

    $boundary = uniqid('np');

    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From: ".$fromName." <".$from.">" . "\r\n"; 
    $headers .= "X-Sender: ".$fromName." <".$from.">\n";
    $headers .= "Return-Path: <".$from.">\n";
    $headers .= "To: ".$to."\r\n";
    $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

    // Content body
    $message = "This is a MIME encoded message.";
    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";

    // Plain text body
    $message .= $text;
    $message .= "\r\n\r\n--" . $boundary . "\r\n";
    $message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

    // Html body
    $message .= $html;
    $message .= "\r\n\r\n--" . $boundary . "--";

    // Send mail
    mail('', $subject, $message, $headers, '-f ' . $from);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多