Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PHP - Send Multipart Email

0 views
Skip to first unread message

luc...@gmail.com

unread,
Jul 3, 2005, 6:03:15 AM7/3/05
to
Hey All,

I'm having trouble trying to create a PHP file which will generate a
multipart email message (containing both an HTML formatted part and a
Plain Text formatted part).

I have Googled for it, and found previous entries in Google Groups, but
I just can't get it to work.

My PHP File contains the following:
<?php

// Generate a Random String for the Email Boundary
$Email_boundary = "==_EmailBoundary_".md5(uniqid())."_==";

$Email_to = 'lukes55...@NOSPAMoptusnet.com.au';
$Email_subject = 'Handover Email';
$Email_body_text = 'Email Body - Text';
$Email_body_html = 'Email Body - <b>HTML</b>';
$Email_body = "\r\n" .
'This is a multi-part message in MIME format.' .
"\r\n" .
"\r\n" .
$Email_boundary . "\r\n" .
'Content-Type: text/plain; charset="iso-8859-1"'
. "\r\n" .
'Content-Transfer-Encoding: 7bit' . "\r\n" .
"\r\n" .
$Email_body_text . "\r\n" .
"\r\n" .
$Email_boundary . "\r\n" .
'Content-Type: text/html; charset="iso-8859-1"'
. "\r\n" .
'Content-Transfer-Encoding: 7bit' . "\r\n" .
'<html>' . "\r\n" .
'<body>' . "\r\n" .
'<p>' . $Email_body_html . '</p>' . "\r\n" .
'</body>' . "\r\n" .
'</html>' . "\r\n" .
$Email_boundary;
$Email_headers = 'From: lukes55...@NOSPAMoptusnet.com.au' .
"\r\n" .
'Reply-To: lukes55...@NOSPAMoptusnet.com.au'
. "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'Content-Type: multipart/alternative; \r\n' .
' boundary="' . $Email_boundary . '"' . "\r\n";

$result = mail($Email_to, $Email_subject, $Email_body,
$Email_headers);
if ( $result ) {
echo "Sent Successfully";
} else {
echo "Failed";
}

?>

The result of the "mail(...)" call is successful and an email is sent,
however when I view it via my Email Client (which can display HTML
pages - Outlook Express), I get:

This is a multi-part message in MIME format.
==_EmailBoundary_b8fc8d0eb39e6b5a5d32561601542430_==
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Email Body - Text
==_EmailBoundary_b8fc8d0eb39e6b5a5d32561601542430_==
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html>
<body>
<p>Email Body - <b>HTML</b></p>
</body>
</html>
==_EmailBoundary_b8fc8d0eb39e6b5a5d32561601542430_==


-------

Any ideas? Help, pointers, wishes of good luck?!?!

glumtail

unread,
Jul 3, 2005, 6:24:33 AM7/3/05
to
MIME is not easy for every person, why not trying PHPMailer?

Andy Hassall

unread,
Jul 3, 2005, 6:38:45 AM7/3/05
to
On 3 Jul 2005 03:03:15 -0700, luc...@gmail.com wrote:

>I'm having trouble trying to create a PHP file which will generate a
>multipart email message (containing both an HTML formatted part and a
>Plain Text formatted part).

> 'Content-Type: multipart/alternative; \r\n' .

You're using single quotes here, so have the literal text '\r\n' in the
header, not a CRLF pair.

Change your code to print the output to the page (in plain text) instead of
emailing it, then you'll be able to spot these errors more easily.

>The result of the "mail(...)" call is successful and an email is sent,
>however when I view it via my Email Client (which can display HTML
>pages - Outlook Express), I get:
>
>This is a multi-part message in MIME format.
>==_EmailBoundary_b8fc8d0eb39e6b5a5d32561601542430_==
>Content-Type: text/plain; charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>Email Body - Text

It's suspicious that there is no blank line between the end of the headers for
the section, and the content of the section.

>==_EmailBoundary_b8fc8d0eb39e6b5a5d32561601542430_==
>Content-Type: text/html; charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
><html>
><body>

Same again.

--
Andy Hassall / <an...@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool

luc...@gmail.com

unread,
Jul 3, 2005, 7:59:54 AM7/3/05
to
Thanks for your help guys. I fixed it (although how is still a bit of a
mystery to me), after going through the source code of a few eBay
multipart emails I have recieved in the past (seems they are one of the
few mass-mailers who use this, rather than assuming that everyone can
read HTML mail).

The final code is:


// Generate a Random String for the Email Boundary

$Email_boundary = "EmailBoundary.".md5(uniqid());

$Email_to = 'reci...@example.com';


$Email_subject = 'Handover Email';
$Email_body_text = 'Email Body - Text';
$Email_body_html = 'Email Body - <b>HTML</b>';

$Email_body = '--' . $Email_boundary . "\r\n" .


'Content-Type: text/plain;
charset="iso-8859-1"' . "\r\n" .
'Content-Transfer-Encoding: 7bit' .
"\r\n" .
"\r\n" .
$Email_body_text . "\r\n" .
"\r\n" .

'--' . $Email_boundary . "\r\n" .


'Content-Type: text/html;
charset="iso-8859-1"' . "\r\n" .
'Content-Transfer-Encoding: 7bit' .
"\r\n" .
'<html>' . "\r\n" .
'<body>' . "\r\n" .
'<p>' . $Email_body_html . '</p>' .
"\r\n" .
'</body>' . "\r\n" .

'</html>';
$Email_headers = 'From: webm...@example.com' . "\r\n" .
'Reply-To: webm...@example.com' . "\r\n" .


'X-Mailer: PHP/' . phpversion() . "\r\n" .

'MIME-Version: 1.0' . "\r\n" .
'Content-Type: multipart/alternative;
boundary=' . $Email_boundary . "\r\n";

mail($Email_to, $Email_subject, $Email_body, $Email_headers);

NOTABLE CHANGES:
Added the MIME-Version to the Header.
Added a "--" before each of the instances of the boundary inside the
email.

It seems to have worked!!!

(Was my first time playing with the insides of Emails, rather than
simply using Plain Text or an Email Client, so at least I learnt
something from the experience...)

Once again, thanks for your replies!

John Dunlop

unread,
Jul 3, 2005, 12:57:42 PM7/3/05
to
Somebody wrote:

> I fixed it (although how is still a bit of a mystery to me), after going
> through the source code of a few eBay multipart emails I have recieved
> in the past

You might learn something that way, and it might even prove
useful to you, but it's not how I would go about learning what
the interworking specifications (RFCs2045-49* among others)
recommend and require. ('MIME-Version: 1.0' is after all an
assertion of your conformance to the rules these documents lay
out.)

Two things: You're missing the close-delimiter ('--' +
boundary + '--') after the last body part, and you're missing
the CRLF pair that should come before the text/html body part.

Tell me again why you're sending text/html.


* http://www.ietf.org/rfc/rfc2045

--
Jock

Manuel Lemos

unread,
Jul 3, 2005, 2:16:19 PM7/3/05
to
Hello,

on 07/03/2005 07:03 AM luc...@gmail.com said the following:


> Hey All,
>
> I'm having trouble trying to create a PHP file which will generate a
> multipart email message (containing both an HTML formatted part and a
> Plain Text formatted part).
>
> I have Googled for it, and found previous entries in Google Groups, but
> I just can't get it to work.

> Any ideas? Help, pointers, wishes of good luck?!?!

You may want to try the MIME message class instead of reinventing the
wheel. Besides composing and sending multipart/alternative messages
correctly with much less effort, it also lets you embed images in the
HTML part or even add attachment files if you want.

http://www.phpclasses.org/mimemessage


--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html

0 new messages