为什么电子邮件在正文/中显示 mime 边界

发布于 2024-10-21 22:33:56 字数 1000 浏览 7 评论 0原文

我正在尝试通过 PHP mail() 发送带有附件的电子邮件。

附件传输正常,正文内容也在那里,但我还看到了 mime 边界和内容类型,而不是电子邮件是 HTML

    --==Multipart_Boundary_x9e92752e972e274a182cc4cafd83c674xContent-Type: text/html; charset="iso-8859-1"Content-Transfer-Encoding: 7bit

<p>
    Body content</p>
<p> <snip>

代码如下所示:

 $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";

    $message = "--{$mime_boundary}Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
    ."Content-Transfer-Encoding: 7bit\r\n\r\n" . $bodyText . "\r\n";   

      $message .=  "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "\n\n";

then the attachment routine

我已经尝试了许多代码变体,但没有成功。毫无疑问,这将是一些我看不到的微不足道的事情。

提前致谢

I am trying to send an email via PHP mail() with attachments.

The attachments come through OK and the content of the body is there but I also see the mime boundary and content type rather than the email being HTML

    --==Multipart_Boundary_x9e92752e972e274a182cc4cafd83c674xContent-Type: text/html; charset="iso-8859-1"Content-Transfer-Encoding: 7bit

<p>
    Body content</p>
<p> <snip>

The code looks like this:

 $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";

    $message = "--{$mime_boundary}Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
    ."Content-Transfer-Encoding: 7bit\r\n\r\n" . $bodyText . "\r\n";   

      $message .=  "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "\n\n";

then the attachment routine

I've tried many variations on the code without success. No doubt it will be something trivial I just can't see.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-10-28 22:33:56

您两次忘记了正确的行结尾。

   $message = "--{$mime_boundary}Content-Type"

第一个 \r\n 丢失。

 $message .=  "--{$mime_boundary}\n" .

这里你只写了\n而不是\r\n

另外不要忘记,您需要在最后有一个终止 MIME 边界,并以双减号作为终止符:

  $message .=  "--{$mime_boundary}--\n" .

You forgot the correct line endings twice.

   $message = "--{$mime_boundary}Content-Type"

There is the first \r\n missing.

 $message .=  "--{$mime_boundary}\n" .

Here you only wrote \n instead of \r\n.

Also don't forget that you need a terminating MIME boundary at the very end with a closing double minus as terminator:

  $message .=  "--{$mime_boundary}--\n" .
柠檬色的秋千 2024-10-28 22:33:56

看起来您在边界定界符之后缺少所需的 CRLF,而且定界符也可能缩进(但一定不能缩进),尽管这可能只是这里的代码格式。看起来您只使用换行符而不是强制的 CRLF。

您应该认真考虑使用成熟的邮件库,例如 SwiftMailer 来轻松地为您组装 MIME 消息。

It looks like you're missing the required CRLF after the boundary delimiter, and also possibly that the delimiter may be indented (which it must not be), though that might just be the code formatting here. It also looks like you're using only newlines instead of the mandatory CRLFs.

You should seriously consider using a mature mailing library, like SwiftMailer to assemble MIME messages for you with ease.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文