How to send a multipart/alternative mail in PHP?

Status
Not open for further replies.

moonvalley

New Member
4
2012
0
0
I am trying to send a multipart/alternative mail in PHP, so that a receiver without HTML mail support can get the plain text version. I can receive the email with the following code, but the content is empty. Can someone please help?

--------------------------------------------------------------------------------

<!DOCTYPE html><html><head></head>
<body><?php

// This creates a random hash for the boundary string
$boundary=md5(time());
$msg="
If you can see this then your client doesn’t accept MIME types!
--$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
This is the simple, alternative mail.
--$boundary
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
<h1>This is the HTML mail.</h1>
--$boundary--
";

mail("fragment_shader@yahoo.com",
"Test 3",
$msg,
"Content-type: multipart/alternative; boundary=\"$boundary\"");

?> </body></html>
 
4 comments
I've checked this script on my server and he was worked improperly too.
When i've add full html markup - i've received correct letter. Please, try this code:

<!DOCTYPE html><html><head></head>
<body><?php

// This creates a random hash for the boundary string
$boundary=md5(time());
$msg="
If you can see this then your client doesn’t accept MIME types!
--$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
This is the simple, alternative mail.
--$boundary
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
<!DOCTYPE html><html><head></head>
<body>
<h1>This is the HTML mail.</h1>
</body></html>
--$boundary--
";

mail("mail@mail.com",
"Test 3",
$msg,
"Content-type: multipart/alternative; boundary=\"$boundary\"");

?>
 
Edit as needed had to lookup on php.net to get the right coding.

Code:
<?
$boundary=md5(time());

$to = $email;
$subject = "";

$headers = "From: email@domain.com" . "\r\n".
            "X-Mailer: PHP/".phpversion() ."\r\n".
            "MIME-Version: 1.0" . "\r\n".
            "Content-Type: multipart/alternative; boundary=$boundary". "\r\n".
            "Content-Transfer-Encoding: 7bit". "\r\n";

    $text = "Plain Text Message";     
    $html = '<html>html message</html>';

$message = "Any message that you want in the header" . "\r\n\r\n".
       "--".$boundary."\r\n".
       "Content-Type: text/plain; charset=\"iso-8859-1\""."\r\n".
       "Content-Transfer-Encoding: 7bit"."\r\n".
       $text."\r\n". 
       "--".$boundary."\r\n". 
       "Content-Type: text/html; charset=\"iso-8859-1\""."\r\n". 
       "Content-Transfer-Encoding: 7bit"."\r\n".
       $html."\r\n".
       "--".$boundary."--"

mail("receiver@email.com", $subject, $message, $headers);
?>
 
Have you tried it yourself? For this I get the following in my mail:

X-Mailer: PHP/5.3.1
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=a74d0ee1fb4e6bbcc10b7bfaad25fa48
Content-Transfer-Encoding: 7bit


Any message that you want in the header

--a74d0ee1fb4e6bbcc10b7bfaad25fa48
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Plain Text Message
--a74d0ee1fb4e6bbcc10b7bfaad25fa48
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html>html message</html>
--a74d0ee1fb4e6bbcc10b7bfaad25fa48--
 
Status
Not open for further replies.
Back
Top