send PHP mail with Content-Type: multipart/alternative

十年热恋 提交于 2019-12-04 03:39:47

问题


I am trying to send a multipart mail that contains both html and plain text. This is also one of the ways to get through spam filters and to allow more people to read the mail in case of not supporting HTML. After spending long hours googling, I have found some examples. I made my code, which sends the mail but it displays the text with the html tags, code, string etc.

<?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 the result:

    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--

As you can see it displays the coding instead of the message alone. I have tried many solutions posted like:

  • adding/removing \r\n;
  • changing \r\n to \n;
  • changing content type from alternative to mixed;

I am learning PHP and all I know is all I have read and done so far. I have still much to learn so please if you could tell me where is the problem. I would be very thankful.Best regards.


回答1:


The line:

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

Has the wrong quotes, so $boundary won't be expanded. Change to:

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

And like I said in the comments, in the message headers and the content section headers you should be using \r\n as the line break since that's what is defined in the RFC. Most MTAs will allow simply \n, but some will choke on the message, and some spam filters will count every RFC violation as a point towards your spam score.

Using something like PHPMailer is a much better option because it formats everything perfectly by default, and abides by just about every single obscure, boring RFC.




回答2:


I think you need quotes around the boundary string.

try this:

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



回答3:


Try this example 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()));


来源:https://stackoverflow.com/questions/18516665/send-php-mail-with-content-type-multipart-alternative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!