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

时间:2013-10-22 09:53:28

标签: php email

我目前在发送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上配置什么才能使其工作?我真的很感激帮助,因为我现在只是碰到砖墙。

1 个答案:

答案 0 :(得分:0)

关于我上面的评论,我强烈建议您使用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--