PHP 邮件发送多部分工作不一致


PHP mail send multipart inconsistently working

我目前在发送PHP多部分电子邮件时遇到问题。我一直在网上寻找我的代码是否正确,它似乎是。我在另一个 Stack 问题中找到了一些代码,并调整了我的代码以确保它与开发人员所说的适合他们的代码相同。以下代码是函数的一部分:

$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x';
$headers  = "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: multipart/alternative; boundary='"$mime_boundary'"'r'n";
$headers .= "Content-Transfer-Encoding: 7bit'r'n";
$replyto .= "reply-to: $replyto";
$body = "This is a multi-part message in mime format.'r'n'r'n";
# Plain text
$body.= "--$mime_boundary'r'n";
$body.= "Content-Type: text/plain; charset='"charset=iso-8859-1'"'r'n";
$body.= "Content-Transfer-Encoding: 7bit'r'n";
$body.= $text_content;
$body.= "'r'n";
# HTML
$body.= "--$mime_boundary'r'n";
$body.= "Content-Type: text/html; charset='"iso-8859-1'"'r'n";
$body.= "Content-Transfer-Encoding: 7bit'r'n";
$body.= $html_content;
$body.= "'r'n";
# End
$body.= "--$mime_boundary--'r'n";
$headers .= "From: $from'r'n";
$headers .= "X-Sender-IP: $_SERVER['SERVER_ADDR']'r'n";
$headers .= 'Date: '.date('n/d/Y g:i A')."'r'n";
$replyto .= "reply-to: $replyto";
# Return
return mail($to, $subject, $body, $headers);

这适用于大多数邮件客户端。它将出现在Mac Thunderbird和Gmail的HTML中。但是,Windows 7上有人使用Thunderbird,它以HTML电子邮件的形式出现,显示html代码而不是HTML电子邮件。

我做错了什么吗?我需要在 Apache 上配置什么才能完成这项工作吗?帮助将不胜感激,因为我现在只是用这个撞砖墙。

关于我上面的评论,我强烈建议您使用 PHPMailer,但是,在我切换 PHPMailer 之前,这是我使用的发送多部分电子邮件的格式:

--52664dd6e3ea3
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hello
--52664dd6e3ea3
Content-Type: multipart/related; type="text/html"; boundary="html52664dd6e3ea3"
--html52664dd6e3ea3
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<strong>Hello</strong>
--html52664dd6e3ea3--
--52664dd6e3ea3--

如果您需要附件:

--52664e536e0b0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hello
--52664e536e0b0
Content-Type: multipart/related; type="text/html"; boundary="html52664e536e0b0"
--html52664e536e0b0
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<strong>Hello</strong>
--html52664e536e0b0
Content-Type: application/octet-stream; name=filename.ext
Content-Transfer-Encoding: base64
Content-Disposition: attachment
{BASE64CODEHERE}
--html52664e536e0b0--
--52664e536e0b0--

这里有多个附件:

--52664e536e0b0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hello
--52664e536e0b0
Content-Type: multipart/related; type="text/html"; boundary="html52664e536e0b0"
--html52664e536e0b0
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<strong>Hello</strong>
--html52664e536e0b0
Content-Type: application/octet-stream; name=filename.ext
Content-Transfer-Encoding: base64
Content-Disposition: attachment
{BASE64CODEHERE}
--html52664e536e0b0
Content-Type: application/octet-stream; name=filename.ext
Content-Transfer-Encoding: base64
Content-Disposition: attachment
{BASE64CODEHERE}
--html52664e536e0b0--
--52664e536e0b0--