Kamil Grzelak
Kamil Grzelak

Reputation: 51

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:

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.

Upvotes: 5

Views: 20559

Answers (4)

Sammitch
Sammitch

Reputation: 32232

The line:

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

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

In addition to this, 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.

So, change to:

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

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.

Upvotes: 5

Strawberry
Strawberry

Reputation: 9

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";
}
?>

Upvotes: 0

Arbuzero
Arbuzero

Reputation: 1

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

Upvotes: 0

chillichief
chillichief

Reputation: 1212

I think you need quotes around the boundary string.

try this:

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

Upvotes: 0

Related Questions