View Full Version : Sending multipart/alternative emails, whats wrong with this code?

02-02-2010, 11:42 AM
I'm trying to send a multipart/alternative email with both the plain text version of the email and the html version in the same send to support older email clients as well as more modern ones.

The code is sending fine however the received email isn't coming through correctly my servers AV is detecting a bad mime error which I believe is causing the problem but I can't see anything wrong with the code at all.
In the final received email it appears to be breaking after the first text/plain content boundry and then just dumping out the rest of the email (including the other mixed headers) as plain text within the body.

This is the code:

$random_hash = md5(date('r', time()));
//add boundary string and mime type specification
$headers = "Return-path: <".$_SESSION['from'].">\r\n";
$headers .= "Reply-to: <".$_SESSION['from'].">\r\n";
$headers .= "From: <".$_SESSION['from'].">\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: Red Limes Sender\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "\r\n\r\n";
$message = "--PHP-alt-$random_hash\r\n
Content-Type: text/plain; charset=\"iso-8859-1\"\r\n
Content-Transfer-Encoding: 7bit\r\n\r\n
Content-Type: text/html; charset=\"iso-8859-1\"\r\n
Content-Transfer-Encoding: 7bit\r\n\r\n

//send the email
mail($to, $subject, $message, $headers);

This is the message details coming through including headers received by server:

RFC822 Message body
Return-Path: <www-data@furious-angels.com>
X-Original-To: hidden@fcsoftware.co.uk
Delivered-To: hidden@fcsoftware.co.uk
Received: from localhost (localhost [])
by mail.furious-angels.com (Postfix) with ESMTP id 923B4B2F29
for <hidden@fcsoftware.co.uk>; Mon, 1 Feb 2010 12:33:44 +0000 (GMT)
X-Quarantine-ID: <blQXIGAyHfe7>
X-Virus-Scanned: Debian amavisd-new at furious-angels.com
X-Amavis-Alert: BAD HEADER MIME error: error: part did not end with expected
Received: from mail.furious-angels.com ([])
by localhost (beta.furious-angels.co.uk []) (amavisd-new, port 10024)
with ESMTP id blQXIGAyHfe7 for <hidden@fcsoftware.co.uk>;
Mon, 1 Feb 2010 12:33:34 +0000 (GMT)
Received: by mail.furious-angels.com (Postfix, from userid 33)
id 39F29B2F22; Mon, 1 Feb 2010 12:33:34 +0000 (GMT)
To: hidden@fcsoftware.co.uk
Subject: test template subject
Reply-to: <hidden@furious-angels.com>
From: <hidden@furious-angels.com>
X-Priority: 3
X-Mailer: Red Limes Sender
Content-Type: multipart/alternative; boundary="PHP-alt-4be183c0dcc161fce6756cbfb9466c46"
MIME-Version: 1.0
Message-Id: <20100201123334.39F29B2F22@mail.furious-angels.com>
Date: Mon, 1 Feb 2010 12:33:34 +0000 (GMT)


Content-Type: text/plain; charset="iso-8859-1"

Content-Transfer-Encoding: 7bit



Content-Type: text/html; charset="iso-8859-1"

Content-Transfer-Encoding: 7bit

<html><body><p>this is a template test</p>
<p> </p>
<p>template test</p>
<p> </p>
<p>template test</p></body></html>


02-02-2010, 03:00 PM
here's a multi part email function that I built a few years back, this may give you some ideas:

// Format and Sends HTML & TXT Email at the same time
function SendHTMLTxTEmail($From, $FromName, $To, $ToName, $Subject, $Text, $Html) {
$Html=$Html?str_replace(array("\r","\n","\t"), "", $Html):preg_replace("/\n/","<br>",$Text)
or die("neither text nor html part present.");
$Text=$Text?$Text:"Sorry, but you need an html mailer to read this mail.";
$From or die("sender address missing");
$To or die("recipient address missing");

$headers ="MIME-Version: 1.0\n";
$headers.="From: ".$FromName." <".$From.">\n";
// $headers.="To: ".$ToName." <".$To.">\n";
$headers.="Reply-To: ".$FromName." <".$From.">\n";
// $headers.="X-Priority: 1\n";
// $headers.="X-MSMail-Priority: High\n";
// $headers.="X-Mailer: My PHP Mailer\n";
$headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";

//Messages start with text/html alternatives in OB
$Msg ="This is a multi-part message in MIME format.\n";
$Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";

//plaintext section
$Msg.="Content-Type: text/plain;\n\tcharset=\"Windows-1252\"\n";
$Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
// plaintext goes here

// html section
$Msg.="Content-Type: text/html;\n\tcharset=\"Windows-1252\"\n";
$Msg.="Content-Transfer-Encoding: base64\n\n";
// html goes here

// end of IB

//message ends


02-03-2010, 03:15 AM
Why not just use PHPMailer. It's much easier

02-03-2010, 03:20 AM
maybe because some people like to know how things actually work instead of just using pre-built scripts.

02-03-2010, 03:29 AM
Of course. I know how to send multipart emails and the whole process behind doing so, I just also like tried and tested classes that save time. If the OP is using your function it's no different to using a class so the argument of wanting to know how it works is a bit of a nonsense argument since he/she can read the source of the class as well as the function

02-03-2010, 03:35 AM
it's not nonsense at all, the class is bloated, where as my function is much more raw making it simpler for him to see what exactly is being done.

02-03-2010, 03:37 AM
Well that's your opinion. It doesn't make your function any better than the class

02-03-2010, 03:43 AM
wow, you've got issues.

I'm really not interested in arguing with you about it. the poster can use whatever he likes, end of discussion.

edit: and for the record, I just checked, PHPMailer is roughly 3500 lines of code. my code is 46 lines. so being bloated is clearly not just an opinion is it? this is easier to go through, specially for someone just learning.

02-03-2010, 03:47 AM
ha that's hilarious. I express my opinion and you don't agree so I have issues :rolleyes: Next I'll be told I'm crazy for indenting my code a certain way.

I could pull your function apart and come up with reasons why it shouldn't be used.
I wasn't saying your function wasn't something to use, I was merely stating that wheels don't need re-inventing every time someone creates a script

And yes the main class is pretty big, theres also a lite version, which is still well documented

02-03-2010, 03:52 AM
yes, but you continued to argue needlessly and I'm really not interested.

and with lines like: "Well that's your opinion. It doesn't make your function any better than the class". right.. and I never said it did, only that it was simpler to read through. but it would appear that you have nothing better to know then argue about nonsense. so yes, I would say that you have some issues if this is what you like to spend your free time doing. I on the other hand have better things to do, so I'll leave it at that.

02-03-2010, 03:54 AM
whatever, I don't give a f***.

Use the function hinch it's amazing

02-03-2010, 06:01 AM
Not to start the argument over but I would recommend using the pair modules as stated in the php manual for sending complex emails...

Yes I'm the type of person who loves knowing how things work and avoid using others' programming when I don't have to. I recently built a multipart email form with an attachment that sends a pdf. I built it originally using php's mail() function but I discovered it only worked in Microsoft Outlook. When I tested with yahoo and gmail the body of the email ended up being blank and even though it said it had an attachment you couldn't open it. I then read on php's website to use the pear modules and it made life sooo much easy. It was a quick fixed once I got them installed.


It's in the notes below the examples. So that said for simple emails use php's mail function complex ones use pair... My two cents ;-)

02-03-2010, 11:31 AM
thanks for the entertaining topic guys.

I refuse to use the phpmailer as on previous unit tests its considerably slower in through put and mails per second sending than just manually writing out using the mail() function thats why I've written this out by hand rather than using pre-built functions/classes etc I need to be a light weight and as little load causing on the server as possible.

I worked out what was happening in the end anyway I'd wrapped $message in just single " " around start and end point the email client was interperating the in code carridge returns as \r\n so was forming the headers and email wrong I've dumped it all down into one line now and it works like a charm.