2
votes

I believe I am sending a correctly formatted multipart/alternative MIME email containing different plain text and HTML versions of content. However, once received by an email client (I've tried 3 different ones), the email's source appears to have been changed; A different boundary text is used and the plain text content has changed to the HTML content (with HTML tags stripped).

This is my PHP script:

<?php
$boundary = md5(date('r', time())); 
$to = '$to_email_address';
$subject = 'Test HTML email'; 
$headers =  "From: $from_email_address\r\n" .
            "Content-Type: multipart/alternative; boundary=\"" . $boundary . "\""; 
$message = "--" . $boundary . "\r\n" .
            "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" . 
            "Content-Transfer-Encoding: 7bit\r\n" .
            "\r\n" .
            "Hello World!!!\r\n" .
            "This is simple text email message.\r\n" .
            "\r\n" .
            "--" . $boundary . "\r\n" . 
            "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" . 
            "Content-Transfer-Encoding: 7bit\r\n" .
            "\r\n" .
            "<h2>Hello World!</h2>\r\n" .
            "<p>This is something with <b>HTML</b> formatting.</p>\r\n" .
            "\r\n" .
            "--" . $boundary . "--\r\n";

$result = mail($to, $subject, $message, $headers);
echo $result ? "Mail sent" : "Mail failed";
?>

and this is the email raw source as seen by email clients:

Return-Path: <from_email_address>
From: <from_email_address>
To: <to_email_address>
Subject: Test HTML email
Date: Fri, 22 Feb 2013 10:22:30 -0000
Message-ID: <01d001ce10e6$8c41d450$a4c57cf0$@com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
    boundary="----=_NextPart_000_01CC_01CE10E6.8C41D450"
X-Mailer: Microsoft Office Outlook 12.0
Thread-Index: Ac4Q5obGqVqbDjA7SDSMnVZDykxxhA==
X-OlkEid: EE843820EF8935D69CB57C40B3E9F64C8874FA01

This is a multi-part message in MIME format.

------=_NextPart_000_01CC_01CE10E6.8C41D450
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


Hello World!


This is something with HTML formatting.


------=_NextPart_000_01CC_01CE10E6.8C41D450
Content-Type: text/html;
    boundary="b04d8008be2af4ce2409beb4e26761bd";
    charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

------=_NextPart_000_01CC_01CE10E6.8C41D450--

How can I ensure that the receiving client receives the intended plain text content?

Thanks in advance for your help, Stuart

2
The Content-Type header in the 2nd body part does not need a boundary parameter. - james.garriss

2 Answers

2
votes

I think I have found the problem. What I was sending was fine. However, I was then moving the email I received between email accounts using Microsoft Outlook in order to view the email source using a different email client (because you cannot view an email's raw source in Outlook) and Outlook was altering my email in the process. Doh! Schoolboy error!

Stuart

-1
votes

For supporting html content in mail. You have to add the following in your header,

$header .="Content-Type: text/html; charset=ISO-8859-1\r\n";

In your code it is,

"Content-Type: multipart/alternative"