472,133 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

PHP - Send Multipart Email

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 = 'l*************@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: lu************@NOSPAMoptusnet.com.au' .
"\r\n" .
'Reply-To: lu************@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?!?!

Jul 17 '05 #1
5 14072
MIME is not easy for every person, why not trying PHPMailer?

Jul 17 '05 #2
On 3 Jul 2005 03:03:15 -0700, lu*****@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
Jul 17 '05 #3
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 = 'r*******@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: we*******@example.com' . "\r\n" .
'Reply-To: we*******@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!

Jul 17 '05 #4
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
Jul 17 '05 #5
Hello,

on 07/03/2005 07:03 AM lu*****@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
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by David Burson | last post: by
1 post views Thread by StevenBarnes | last post: by
5 posts views Thread by Thierry | last post: by
10 posts views Thread by Mike Charney | last post: by
9 posts views Thread by Mahernoz | last post: by
1 post views Thread by Wells Wang | last post: by
4 posts views Thread by Tony M | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.