使用Content-Type发送PHP邮件:multipart/alternative

owfi6suc  于 2023-10-15  发布在  PHP
关注(0)|答案(4)|浏览(141)

我正在尝试发送一个包含html和纯文本的多部分邮件。这也是通过垃圾邮件过滤器的方法之一,并允许更多的人在不支持HTML的情况下阅读邮件。经过长时间的谷歌搜索,我找到了一些例子。我做了我的代码,它发送邮件,但它显示的文本与html标签,代码,字符串等。

<?php
$boundary=md5(uniqid(rand()));
$header .= "From:My Name<[email protected]>\n";
$header .= "Reply-To: [email protected] \n";
$header .= 'MIME-Version: 1.0'."\r\n";
$header .= 'Content-type: multipart/alternative;boundary=$boundary '."\n";

$adres = "[email protected]";

$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,到目前为止,我所知道的就是我所读过和做过的。我还有很多东西要学,所以请你告诉我问题在哪里。我会非常感激的。最好的问候。

ma8fv8wu

ma8fv8wu1#

线路:

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

有错误的引号,所以$boundary不会被展开。
除此之外,正如我在评论中所说的,在消息头和内容部分头中,您应该使用\r\n作为换行符,因为这是RFC中定义的。大多数MTA只允许\n,但有些会阻塞消息,有些垃圾邮件过滤器会将每个RFC违规作为垃圾邮件得分的一个点。
因此,更改为:

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

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

7uzetpgm

7uzetpgm2#

我认为你需要在边界字符串周围加上引号。
试试这个:

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

mxg2im7a3#

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

$m = new PhpMimeClient();
// Add to
$m->addTo("[email protected]", "Albercik");
$m->addTo("[email protected]", "Adela");
// Add Cc
$m->addCc("[email protected]");
// Add Bcc
$m->addBcc("[email protected]", "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", "[email protected]");
// get mime
// $m->getMime();
// Show mime
echo nl2br(htmlentities($m->getMime()));
tcomlyy6

tcomlyy64#

以下是完整的脚本,没有错误:

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

$mailto = "[email protected]";
$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";
}
?>

相关问题