我正在尝试发送包含 html 和纯文本的多部分邮件。这也是通过垃圾邮件过滤器并在不支持HTML的情况下允许更多人阅读邮件的方法之一。经过长时间的谷歌搜索,我找到了一些例子。我编写了我的代码,它发送邮件,但它显示带有 html 标签、代码、字符串等的文本。
<?php
$boundary=md5(uniqid(rand()));
$header .= "From:My Name<something@something.com>\n";
$header .= "Reply-To: something@something.com \n";
$header .= 'MIME-Version: 1.0'."\r\n";
$header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";
$adres = "something@gmail.com";
$subject = "subject";
$message = "This is multipart message using MIME\n";
$message .= "--" . $boundary . "\n";
$message .= "Content-type: text/plain;charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .= "Plain text version\n\n";
$message .="--" . $boundary . "\n";
$message .="Content-type: text/html;charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .="<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>\n\n";
$message .= "--" . $boundary . "--";
if(mail($adres, $subject, $message, $header))
{
print'message sent';
}
else
{
print'message was not sent';
}
?>
这是结果:
This is multipart message using MIME
--c071adfa945491cac7759a760ff8baeb
Content-type: text/plain;charset=iso-8859-1
Content-Transfer-Encoding: 7bit
Plain text version
--c071adfa945491cac7759a760ff8baeb
Content-type: text/html;charset=iso-8859-1
Content-Transfer-Encoding: 7bit
<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>
--c071adfa945491cac7759a760ff8baeb--
如您所见,它显示编码而不是单独显示消息。我尝试了许多发布的解决方案,例如:
- 添加/删除 \r\n;
- 将 \r\n 更改为 \n;
- 将内容类型从替代更改为混合;
我正在学习 PHP,我所知道的就是我到目前为止所阅读和所做的一切。我还有很多东西要学,所以请告诉我问题出在哪里。我将非常感谢。最好的问候。