1

我目前在发送 PHP 多部分电子邮件时遇到问题。我一直在网上查看我的代码是否正确,并且看起来是正确的。我在另一个 Stack question 上找到了一些代码,并调整了我的代码以确保它与开发人员所说的为他们工作的代码相同。以下代码是函数的一部分:

$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);

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

有什么我做错了吗?我需要在 Apache 上配置什么才能使其工作吗?非常感谢您的帮助,因为我现在只是在碰砖头。

4

1 回答 1

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--
于 2013-10-22T10:08:32.960 回答