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

PHP邮件发送多部分工作不一致,php,email,Php,Email,我目前在发送PHP多部分电子邮件时遇到问题。我一直在网上查看我的代码是否正确,它似乎是正确的。我在另一个堆栈问题上找到了一些代码,并调整了我的代码,以确保它与开发人员所说的为他们工作的代码相同。以下代码是函数的一部分: $mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x'; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/alternat

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

$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中。然而,有人在Windows7上使用Thunderbird,它是作为HTML电子邮件发送的,显示的是HTML代码,而不是HTML电子邮件


我做错什么了吗?我需要在Apache上配置什么来实现这一点吗?非常感谢您的帮助,因为我目前正在使用此功能。

关于我的上述评论,我强烈建议您使用,但是,在我切换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--
——52664dd6e3ea3
内容类型:文本/纯文本;字符集=ISO-8859-1
内容传输编码:引用可打印
你好
--52664dd6e3ea3
内容类型:多部分/相关;type=“text/html”;boundary=“html52664dd6e3ea3”
--html52664dd6e3ea3
内容类型:text/html;字符集=ISO-8859-1
内容传输编码:引用可打印
你好
--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
内容类型:文本/纯文本;字符集=ISO-8859-1
内容传输编码:引用可打印
你好
--52664e536e0b0
内容类型:多部分/相关;type=“text/html”;boundary=“html52664e536e0b0”
--html52664e536e0b0
内容类型:text/html;字符集=ISO-8859-1
内容传输编码:引用可打印
你好
--html52664e536e0b0
内容类型:应用程序/八位字节流;name=filename.ext
内容传输编码:base64
内容配置:附件
{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--
--52664e536e0b0
内容类型:文本/纯文本;字符集=ISO-8859-1
内容传输编码:引用可打印
你好
--52664e536e0b0
内容类型:多部分/相关;type=“text/html”;boundary=“html52664e536e0b0”
--html52664e536e0b0
内容类型:text/html;字符集=ISO-8859-1
内容传输编码:引用可打印
你好
--html52664e536e0b0
内容类型:应用程序/八位字节流;name=filename.ext
内容传输编码:base64
内容配置:附件
{BASE64CODEHERE}
--html52664e536e0b0
内容类型:应用程序/八位字节流;name=filename.ext
内容传输编码:base64
内容配置:附件
{BASE64CODEHERE}
--html52664e536e0b0--
--52664e536e0b0--

我可以花一点时间查找代码的问题,或者您可以切换到PHPMailer,永久解决问题。有些东西不值得重新发明,我真的建议使用PHPMailer而不是构建自己的内容hi@JamieBicknell,我明白你的意思。我一直在处理遗留代码和服务器迁移。我明白你的意思,我会看看PHPMailer。绝对同意@JamieBicknell。PHP内置的
mail()
函数是一个很难处理的问题,它很脆弱,而且常常不安全。你真的应该尽快切换到PHPMailer(或Swiftmailer)。谢谢你的提示-我现在正在切换。