problems with creating both HTML and plaintext emails

This is a discussion on problems with creating both HTML and plaintext emails within the PHP Language forums, part of the PHP Programming Forums category; Before I begin, I realise there's a big war regarding HTML in e-mails. Personally, I don't like ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-21-2005
news@celticbear.com
 
Posts: n/a
Default problems with creating both HTML and plaintext emails

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.";
?>

Reply With Quote
  #2 (permalink)  
Old 01-21-2005
Alan Connor
 
Posts: n/a
Default Re: problems with creating both HTML and plaintext emails

On 20 Jan 2005 15:41:50 -0800, news@celticbear.com
<news@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/groups?selm...oglegroups.com

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

#
# From: no_reply@domain.com
# To: recipient@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

Reply With Quote
  #3 (permalink)  
Old 01-21-2005
Sam
 
Posts: n/a
Default Re: problems with creating both HTML and plaintext emails

news@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-----

Reply With Quote
  #4 (permalink)  
Old 01-21-2005
Sam
 
Posts: n/a
Default Re: problems with creating both HTML and plaintext emails

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/groups?selm...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-----

Reply With Quote
  #5 (permalink)  
Old 01-21-2005
Jem Berkes
 
Posts: n/a
Default Re: problems with creating both HTML and plaintext emails

> 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/
Reply With Quote
  #6 (permalink)  
Old 01-21-2005
news@celticbear.com
 
Posts: n/a
Default Re: problems with creating both HTML and plaintext emails

Sam wrote:
> news@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.";

?>

Reply With Quote
  #7 (permalink)  
Old 01-22-2005
Kartic
 
Posts: n/a
Default Re: problems with creating both HTML and plaintext emails

news@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
Reply With Quote
  #8 (permalink)  
Old 01-22-2005
Sam
 
Posts: n/a
Default Re: problems with creating both HTML and plaintext emails

news@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-----

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:26 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0