【问题标题】:PHP mail send multipart inconsistently workingPHP邮件发送多部分不一致的工作
【发布时间】:2013-10-22 09:53:28
【问题描述】:

我目前在发送 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 上配置什么才能使其工作吗?非常感谢您的帮助,因为我现在只是在碰壁。

【问题讨论】:

  • 我可以花一点时间找出您的代码问题,或者您可以切换到 PHPMailer 并永久解决问题。有些东西不值得重新发明,我真的建议使用 PHPMailer 而不是构建自己的内容
  • 嗨@JamieBicknell,我明白你在说什么。我一直在处理遗留代码和服务器迁移。我接受你所说的,我会看看 PHPMailer。
  • 完全同意@JamieBicknell。 PHP 的内置mail() 函数使用起来真的很痛苦,它很不稳定,而且通常不安全。你真的应该尽快切换到 PHPMailer(或 Swiftmailer)。
  • 感谢您的提示 - 我现在正在切换内容。

标签: php email


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多