473,246 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 14186
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David Burson | last post by:
Hi, I have a VB.NET windows app that needs to automatically send a simple text email when my users run a new version of the app for the first time. I thought this would be simple, but after...
1
by: StevenBarnes | last post by:
I need to have some .Net code be able to send Email using my ISP's SMTP server. I found some articles on how to use the MAIL FROM: and RCPT TO: commands that you can stream to a tcp connection on...
5
by: Thierry | last post by:
Hello. Is it possible to send an email with some HTML code ? I tried the following code, where $msg == '<html><body>... blah blab blah etc.</body></html>' : // Adresse de l'expediteur...
10
by: Mike Charney | last post by:
Is there a simple way to send SMTP email from Access VBA? Mike m charney at dunlap hospital dot org
9
by: Mahernoz | last post by:
Can i send an email from JavaScript? Is it possible? If yes please the code to send email using javascript...
1
by: Wells Wang | last post by:
Hi, Everybody: Sorry to bother you. I am trying to use the code below to send an email with asp.net2.0& win XP. But it failed. On my laptop I can use outlook2003 to send email. If you are...
4
Frinavale
by: Frinavale | last post by:
Introduction Many .NET applications will require an email be sent out for various reasons. This article will give a quick examples on how to send an email using VB.NET. The examples given can...
4
by: Tony M | last post by:
VS 2005 - XP media - VB .net - winforms - .net 2.0 Just trying to send an email, here is the code and the error message that I get. I can't figure out how to fix it?
3
by: fishnfrogs | last post by:
Howdy ya'll!! I'm using PHPMailer to send out a multipart email. My first question is what is the best way to test the alternate text? I've tried sending it to different email accounts to see if...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.