Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send PHP mail with Content-Type: multipart/alternative

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

like image 760
Kamil Grzelak Avatar asked Aug 29 '13 16:08

Kamil Grzelak


2 Answers

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.

like image 151
Sammitch Avatar answered Sep 26 '22 05:09

Sammitch


Here is the complete script without errors:

<?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";
}
?>
like image 20
Strawberry Avatar answered Sep 25 '22 05:09

Strawberry