Page 1 of 1

multi mime encoded mail messages with php

Posted: Sun Jan 18, 2004 2:25 am
by itbegary
I have the section of code included below. It is a simple piece that I have gathered from a mail.mime group as to the sending of email in multiple formats. My goal here is simple. I need to send both a clear text formatted message and an HTML message in the same email. Including an image would also be useful but not required.

Any clear cut "functional" examples would be greatly appriciated. I have downloaded a few that just don't seem to work. The targets are Outlook (html format) and Horde/Imp (for the text format). I know, it sounds odd but these are the two clients that we need to make the format work on.

Code: Select all

<?php
// generate MIME boundary 
$boundary = "---".md5(uniqid(time())).""; 

// set email headers 

$header = "Return-Path: theo@primeexalia.com\r\n"; 
$header.= "From: Theo <theo@primeexalia.com>\r\n"; 
$header.= "MIME-Version: 1.0\r\n"; 
$header.= "Content-Type: multipart/mixed; boundary="$boundary"\r\n\r\n"; 

// set text & html headers 

$txt_head = $boundary."\r\n"; 
$txt_head.= "Content-Type: text/plain;\r\n";
$txt_head.= "Content-Transfer-Encoding: quoted-printable;\r\n\r\n";
$txt_head.= "some simple text\r\n\r\n";


$htm_head = $boundary."\r\n"; 
$htm_head.= "Content-Type: text/html;\r\n";
$htm_head.= "Content-Transfer-Encoding: quoted-printable;\r\n\r\n";
$htm_head.= "<html><body><font size=3>Hello</font></body></html>\r\n\r\n";

// combine headers & message 
$mesg = $htm_head.$txt_head.$boundary; 

// send email out 

mail ("theo@primeexalia.com","test with dual encoding",$mesg,$header);
?>

Posted: Sun Jan 18, 2004 10:56 am
by mikusan
Ha, Ha, Ha...

I was working on getting this to work for more than a day. I realized that there is no simple answer, In fact, i tried and retried and retried.

Outlook would display it one way, online email clients (Squirrelmail and such) would display it in another. Then at some point some would block it alltogether because the page is non-secure.

I never got it to work although i am curious as to how was anyone been able to get it to work.

NOTE: The code you got there is much better looking than mine (after millions of changes it looked like a monkey on steroids), however, Do NOT use multipart/mixed with HTML code that includes any images. Outlook will not load the image, and other browesers will quarantine your message.

Also, you need to add "--" before your boundaries.
So, since your code looks like: "---".md5(uniqid(time()))."";

your real boundaries should be like:
"-----".md5(uniqid(time())).""; Note the extra "--"

Let me know if you can get it to work reliably. I would like to fix mine myself.

Posted: Sun Jan 18, 2004 11:05 am
by Gen-ik
mikusan wrote:Do NOT use multipart/mixed with HTML code that includes any images. Outlook will not load the image, and other browesers will quarantine your message.
A correctly built mime-based email can contain plain-text, HTML, images, and attachments... you just need to build the email headers and body correctly (which varies depending on the content of the email).

Normally a PHP class() for creating mime emails are huge, and the code posted above doesn't even scratch the surface of what's needed.

I have created my own mime-mail class() which works fine, if I have time I will dig it out and post it here but in the mean time I would suggest looking around for a pre-built mime email class() and use that.

Posted: Sun Jan 18, 2004 12:07 pm
by mikusan
Yeah, my warning of not using images in emails is due to the fact that i could not get it to work. So my suggestion was not to use images until you actually got the thing to work. Because that's one less variable to worry about.

Nevertheless, thank you for pointing me in the right direction, i looked around but the best that i could find is the following:
http://www.hotscripts.com/Detailed/4682.html

Although i don't have the time right now to look at it, the description does not reflect what i was looking for, nor the OP.

If you still have that multipart/mixed class lying around, that would be awesome.

Posted: Sun Jan 18, 2004 12:09 pm
by markl999
PHPMailer might be what you're after, it's one of the best at doing that sort of thing imho.

Posted: Sun Jan 18, 2004 12:11 pm
by mikusan
Gen-ik wrote:
A correctly built mime-based email can contain plain-text, HTML, images, and attachments... you just need to build the email headers and body correctly (which varies depending on the content of the email).
About that, is text/html incorrect for the header tag. Apart the fact that i still can't get it to work, i got pretty close and found that when i just use normal headers and HTML based email, the email displays correctly, however, when i try to port my email content to the multipart email, the images do not load properly.

I am not sure why, maybe some extra security measures by email clients that do not want http:// links in some types of emails.

Then again i have no idea why it's not working.

Posted: Sun Jan 18, 2004 2:36 pm
by itbegary
Using the code that I posted I can go to Imp and see the headers and they all look correct. I have tried varients for the boundary (that's just the one I ended with).

What is interesting is that in Outlook (and OWA) I get the whole thing as an attachment. This is the attachment contents.

Please note that I don't have a problem sending HTML or Text but rather the combination.

Code: Select all

boundary="-24777e2c3d37cb951473f5b7fb278616-"

-24777e2c3d37cb951473f5b7fb278616-
Content-Type: text/html;
Content-Transfer-Encoding: quoted-printable;

<html><body><font size=3>Hello</font></body></html>

-24777e2c3d37cb951473f5b7fb278616-
Content-Type: text/plain;
Content-Transfer-Encoding: quoted-printable;

some simple text

-24777e2c3d37cb951473f5b7fb278616-