5

我正在尝试发送包含 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,我所知道的就是我到目前为止所阅读和所做的一切。我还有很多东西要学,所以请告诉我问题出在哪里。我将非常感谢。最好的问候。

4

4 回答 4

4

该行:

$header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";

有错误的引号,所以$boundary不会被扩展。改成:

$header .= "Content-type: multipart/alternative;boundary=$boundary\n";

就像我在评论中所说的那样,在邮件标题和内容部分标题中,您应该将其\r\n用作换行符,因为这是 RFC 中定义的。大多数 MTA 会简单地允许\n,但有些会阻塞邮件,并且一些垃圾邮件过滤器会将每次 RFC 违规都计为垃圾邮件分数。

使用像PHPMailer这样的东西是一个更好的选择,因为它默认完美地格式化所有内容,并且遵守几乎每一个晦涩、无聊的 RFC。

于 2013-08-29T18:12:24.413 回答
1

这是没有错误的完整脚本:

<?php
error_reporting(-1);
ini_set("display_errors", "1");

$mailto = "email@enter-domainname-here.com";
$subject = "subject";

$boundary=md5(uniqid(rand()));
$header = "From:Info<".$mailto.">\n";
$header .= "Reply-To: ".$mailto."\n";
$header .= "MIME-Version: 1.0"."\n";
$header .= "Content-type: multipart/alternative; boundary=\"----=_NextPart_" . $boundary . "\"";

$message = "This is multipart message using MIME\n";

$message .= "------=_NextPart_" . $boundary . "\n";
$message .= "Content-Type: text/plain; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";
$message .= "Plain text version\n\n";
$message .="------=_NextPart_" . $boundary . "\n";
$message .="Content-Type: text/html; charset=UTF-8\n";
$message .= "Content-Transfer-Encoding: 7bit". "\n\n";

$message .="<html>
<body>
<center>
<b>HTML text version</b>
</center>
</body>
</html>\n\n";
$message .= "------=_NextPart_" . $boundary . "--";

if(@mail($mailto, $subject, $message, $header))
{
print'message sent';
}
else
{
print"message was not sent";
}
?>
于 2020-01-05T11:20:23.130 回答
0

我认为您需要在边界字符串周围加上引号。

尝试这个:

$header    .= 'Content-type: multipart/alternative; boundary="' . $boundary . '"\r\n';
于 2015-03-24T13:13:55.980 回答
0

试试这个例子https://github.com/breakermind/PhpMimeParser/blob/master/PhpMimeClient_class.php

$m = new PhpMimeClient();
// Add to
$m->addTo("email@star.ccc", "Albercik");
$m->addTo("adela@music.com", "Adela");
// Add Cc
$m->addCc("zonk@email.au");
// Add Bcc
$m->addBcc("boos@domain.com", "BOSS");    
// Add files inline
$m->addFile('photo.jpg',"zenek123");
// Add file
$m->addFile('sun.png');
// create mime
$m->createMime("Witaj!",'<h1>Witaj jak się masz? <img src="cid:zenek123"> </h1>',"Wesołych świąt życzę!","Heniek Wielki", "hohoho@domain.com");
// get mime
// $m->getMime();
// Show mime
echo nl2br(htmlentities($m->getMime()));
于 2017-11-06T19:26:34.197 回答