By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

problems with creating both HTML and plaintext emails

P: n/a
Before I begin, I realise there's a big war regarding HTML in e-mails.
Personally, I don't like it. Unfortunately, I'm being paid by my
company to create an automated newsletter for our customers.
I did all I could just to get them to allow me to have it sent in both
plaintext and HTML so that the e-mail clients have a choice.

Anyway. I've looked around the RFC's and the source of similar e-mails,
and I'm certain I figured out how to do it...but there's still some
problems I can't figure out.

Below will be the PHP script I'm using to create the message.
But what's happening is that it appears to work fine in the major
E-mail clients like Outlook and Thunderbird, but doesn't work in Kmail
or Evolution or other clients.

What am I doing wrong?
Thanks for any advice!
Liam

<?php
// PAGE CREATED 2004-06-01 by Liam
/*################################################
### DETERMINE DAY APPROPRIATENESS
################################################## */
$today_day = date("w"); // Day of the week numeric: Sun=0,
Mon=1...Sat=6
$today = date("Y-m-d");
/*################################################
### RUN PROCESS
################################################## */

$to="Recipient Name <my-email-addy>";
$from="Sender Name <another-addy>";
$subject="My first HTML E-mail";
$mime_boundary="==Multipart_Boundary_x".md5(mt_ran d())."x";
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type:multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\r\n\"";
$headers.= "From: $from\r\n";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Disposition: inline\n".
"Content-Transfer-Encoding: 7bit\n".
"Content-Type: text/plain\n\n".
"The plaintext message goes here!
" .
"--{$mime_boundary}\n" .
"Content-Disposition: inline\n".
"Content-Transfer-Encoding: 7bit\n".
"Content-Type: text/html\n\n".
"<html>
<head>
<title>HTML MSG HERE</title>
</head>
<body bgcolor=\"#ffffff\">
</body>
</html>
".
"--{$mime_boundary}\n
";
if (mail($to, $subject, $message, $headers))
echo "Message Sent!";
else
echo "Failed to send message.";
?>

Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 20 Jan 2005 15:41:50 -0800, ne**@celticbear.com
<ne**@celticbear.com> wrote:
Before I begin, I realise there's a big war regarding HTML
in e-mails. Personally, I don't like it. Unfortunately, I'm
being paid by my company to create an automated newsletter for
our customers. I did all I could just to get them to allow me
to have it sent in both plaintext and HTML so that the e-mail
clients have a choice.
Good work there.

I don't accept HTML mails, and I am hardly alone in this choice.

In some cases, like an initial contact, I'll (my filter will)
send the body through a utility to convert it into plain text,
dumping images, soundfiles, and attachments.

Anyway. I've looked around the RFC's and the source of similar
e-mails, and I'm certain I figured out how to do it...but
there's still some problems I can't figure out.

Below will be the PHP script I'm using to create the message.
But what's happening is that it appears to work fine in the
major E-mail clients like Outlook and Thunderbird, but doesn't
work in Kmail or Evolution or other clients.

What am I doing wrong? Thanks for any advice! Liam


<snip>

I don't read PHP, News, or generally send html mails, but here's
a link to a post that you might find useful:

http://groups.google.com/gr*********...oglegroups.com

And here's all that's needed to send an in-line html mail:

#
# From: no******@domain.com
# To: re*******@whatever.com
# Subject: Whatever
# Content-Type: text/html
# Content-Transfer-Encoding: 7bit
# Content-Disposition: inline
#
# <BODY>
#
# rest of the html
#
#
# </BODY>
# .
#
#
AC
--
Pro-Active Spam Fighter
Pass-list --> Spam-Filter --> Challenge-Response
http://tinyurl.com/2t5kp

Jul 17 '05 #2

P: n/a
Sam
ne**@celticbear.com writes:
Below will be the PHP script I'm using to create the message.
But what's happening is that it appears to work fine in the major
E-mail clients like Outlook and Thunderbird, but doesn't work in Kmail
or Evolution or other clients.

What am I doing wrong? "Content-Type:multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\r\n\"";
Bug #1. For starters, you're using LF everywhere else, and here you have
CRLF. Furthermore, it's misplaced. It should be after the closing quote.

You'll have to investigate whether PHP's mail() functions expects all lines
to be terminated by LF, or CRLF. I don't recall offhand. Then you must
change your code and use a consistent EOL.
<title>HTML MSG HERE</title>
</head>
<body bgcolor=\"#ffffff\">
</body>
</html>
".
"--{$mime_boundary}\n
";


Bug #2. The closing boundary delimiter should have a trailing "--".


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBB8FTVx9p3GYHlUOIRAnjcAJ9zk5YHpb8DRelZplq/UzP5ayI5jwCZAT9v
t8TQH2ZX8umJuK09IsGSRRQ=
=En7E
-----END PGP SIGNATURE-----

Jul 17 '05 #3

P: n/a
Sam
Beavis writes:
Good work there.

I don't accept HTML mails, and I am hardly alone in this choice.
Nobody asked you.

In some cases, like an initial contact, I'll (my filter will)
send the body through a utility to convert it into plain text,
dumping images, soundfiles, and attachments.
Nobody cares, Beavis.
<snip>

I don't read PHP, News, or generally send html mails, but here's
a link to a post that you might find useful:

http://groups.google.com/gr*********...oglegroups.com

And here's all that's needed to send an in-line html mail:


Beavis, you ignorant slut. He wants to know how properly format
multipart/alternative MIME content, and not just HTML mail.

If you don't understand what someone's asking, just keep your yap shut and
let your mental superiors handle it.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBB8FVex9p3GYHlUOIRArmuAJ9QquO3S1NuU9+lOfpwea GUvN7uqwCdHYmC
VQIlhBv0lkmenYZcueRnOPg=
=uTQz
-----END PGP SIGNATURE-----

Jul 17 '05 #4

P: n/a
> Anyway. I've looked around the RFC's and the source of similar e-mails,
and I'm certain I figured out how to do it...but there's still some
problems I can't figure out.


Besides the technical side of creating the content, you should of course
also test your output with a number of popular readers to see what it looks
like! I would suggest,

hotmail (web)
gmail (web0
Outlook
Eudora
Pegasus Mail
Mozilla Thunderbird

It's a nagging problem with design for presentation... you had better
really see what the result looks like :)

--
Jem Berkes
Windows, UNIX software and system design
http://www.sysdesign.ca/
Jul 17 '05 #5

P: n/a
Sam wrote:
ne**@celticbear.com writes:
What am I doing wrong?
"Content-Type:multipart/alternative;\n" .
" boundary=\"{$mime_boundary}\r\n\"";


Bug #1. For starters, you're using LF everywhere else, and here you

have CRLF. Furthermore, it's misplaced. It should be after the closing quote.
You'll have to investigate whether PHP's mail() functions expects all lines to be terminated by LF, or CRLF. I don't recall offhand. Then you must change your code and use a consistent EOL.


OK, Below is my updated script.
\n is the appropriate EOL, so I've fixed that as well as where the
quotes were. That was just bad on my part.
".
"--{$mime_boundary}\n
";


Bug #2. The closing boundary delimiter should have a trailing "--".


And you'll see I added that too.
I also looked around at more source code of my successfully received
newsletter e-mails and am trying to emulate them, so I made a couple of
other changes.
And yet, still doesn't work.
No matter what e-mail client I use, the source code for the e-mail
looks fine, it's all there...but nothing is visible in the e-mail
display.
In Kmail, it show a brief summary of the parts the e-mail includes, and
on mine it always says:

e-mail type: multipart/alternative 8bit (that's odd considering I use
7bit in the code) 19.6 KB
body part: Plain Text Document 0 KB

E-mails that work always has a 2nd "body part" as HTML listed, and
obviously, the appropriate size. Why mine seems to cut off detection at
the plain text doc and not have the size, I can't fathom. Everything
appears the same as working e-mails.

Anyway, thanks for the replies, and any tips are appreciated!
Liam

<?php
// PAGE CREATED 2004-06-01 by Liam
$today_day = date("w"); // Day of the week numeric: Sun=0,
Mon=1...Sat=6
$today = date("Y-m-d");

$to="Recipient Name <e-mail-addy>";
$from="Sender Name <another-email-addy>";
$subject="My first HTML E-mail";
$mime_boundary="Multipart-Boundary-".md5(mt_rand());
$headers = "From: $from\n" .
"MIME-Version: 1.0\n" .
"Content-Type:multipart/alternative;\n" .
" boundary=\"--{$mime_boundary}--\"\n";
$headers.= "From: $from\n";
$message = "--{$mime_boundary}--\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n".
"Content-Disposition: inline\n".
"Content-Transfer-Encoding: 7bit\n\n".
"(PLAIN TEXT DOC GOES HERE)" .
"--{$mime_boundary}--\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n".
"Content-Disposition: inline\n".
"Content-Transfer-Encoding: 7bit\n\n".
"<html>

<head>
<title>Welcome to E-Boot Camp Phase I</title>
</head>

<body bgcolor=\"#ffffff\">
(HTML DOC GOES IN HERE)
</body>
</html>
".
"--{$mime_boundary}---\n\n\n

";
if (mail($to, $subject, $message, $headers))
echo "Message Sent!";
else
echo "Failed to send message.";

?>

Jul 17 '05 #6

P: n/a
ne**@celticbear.com said the following on 1/20/2005 6:41 PM:
Before I begin, I realise there's a big war regarding HTML in e-mails.
Personally, I don't like it. Unfortunately, I'm being paid by my
company to create an automated newsletter for our customers.
I did all I could just to get them to allow me to have it sent in both
plaintext and HTML so that the e-mail clients have a choice.

Anyway. I've looked around the RFC's and the source of similar e-mails,
and I'm certain I figured out how to do it...but there's still some
problems I can't figure out.

Below will be the PHP script I'm using to create the message.
But what's happening is that it appears to work fine in the major
E-mail clients like Outlook and Thunderbird, but doesn't work in Kmail
or Evolution or other clients.

What am I doing wrong?
Thanks for any advice!
Liam


Liam - Why don't you take a look at phpmailer available at
http://phpmailer.sourceforge.net/ ?

It is a PHP class that helps you create MIME emails, including emails
with HTML and also build an alternative plain text part for MUAs that do
not read HTML.

This class is good and will save you time building HTML emails.

Of course, as Jem mentioned, test your sample emails with the prominent
free email services.

HTH.

Thanks,
--Kartic
Jul 17 '05 #7

P: n/a
Sam
ne**@celticbear.com writes:
> ".
> "--{$mime_boundary}\n
> ";


Bug #2. The closing boundary delimiter should have a trailing "--".


And you'll see I added that too.


No you didn't. You added trailing --s to every boundary delimiter, not just
the closing one.

You should review the RFCs, and use this message as an example of how to
correctly set the delimiters for a multipart MIME message.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBB8azqx9p3GYHlUOIRApYyAJ4lg8QhtXQxbEYSyYLOMk Wf1cUhOgCdFMzU
/5B/TI6X+oeScmiCZwqglS0=
=uWba
-----END PGP SIGNATURE-----

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.