Page 1 of 1

problems with mime mail

Posted: Sun Aug 23, 2009 7:27 pm
by arminium
Hi guys i have been working on this mime email with pdf invoice attached for a while, sooo much trouble with it, and i always seem to have trouble with mime emails with attachments.

I have it working in Thunderbird (comes with attachment perfect) but in windows mail the pdf attachment comes thru as a .txt which means all the content in garble

the mail setup is as follows and below is the output txt file in windows mail (i have omited about 10000 lines of garble in the middle )

also if anyone knows of a good written explaination of mime headers, a link would be awesome, every thing i read is soooo different

Thanx in advance

Code: Select all

$attachment = chunk_split(base64_encode($stream));
$to  = $pf['email'];
$subject = 'Your Invoice';
 
$random_hash = md5(date('r', time()));
$headers  = "From: ".SITE_NOREPLY." \r\n";
$headers .= "Reply-To: ".SITE_EMAIL." \r\n";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
 
 
$message = "
--PHP-mixed-". $random_hash ." 
Content-Type: multipart/alternative; boundary=\"PHP-alt-". $random_hash ."\"
 
--PHP-alt-". $random_hash ." 
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
 
Hi $name
 
Please find Attached your most recent Invoice that is ready for payment
 
--PHP-alt-". $random_hash ." 
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
 
<h2>Invoice</h2>
<p>Attached Is the Invoice for Your Latest Order From Company</p>
 
--PHP-alt-". $random_hash ." 
 
--PHP-mixed-". $random_hash ."  
Content-Type: application/pdf; name=\"invoice-".$invid.".pdf\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 
 
".$attachment."
--PHP-mixed-". $random_hash ." --";

The ouput in windows mail is "att0042.txt"
--PHP-mixed-1783d3a01d99ae0a0bfafb11ad40cc94
Content-Type: application/pdf; name="invoice-2500.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

JVBERi0xLjcKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL01lZGlhQm94IFsw
IDAgNTk1LjI4IDg0MS44OV0KL1Jlc291cmNlcyAyIDAgUgovQW5ub3RzIFsKPDwvVHlwZSAvQW5u
b3QgL1N1YnR5cGUgL0xpbmsgL1JlY3QgWzU2LjY5IDc5OS4zNyAyODMuNDYgNzA4LjY2XSAvQ29u
dGVudHMgKP7/AGgAdAB0AHAAOgAvAC8AdwB3AHcALgBnAGEAdABlAC0AcwB5AHMAdABlAG0AcwAu
mUgMjEKL1Jvb3QgMjAgMCBSCi9JbmZvIDE5IDAgUgo+PgpzdGFydHhyZWYKNzA5NTQyCiUlRU9GC
g==


--PHP-mixed-1783d3a01d99ae0a0bfafb11ad40cc94 --

Re: problems with mime mail

Posted: Mon Aug 24, 2009 4:49 pm
by arminium
SOLVED

Re: problems with mime mail

Posted: Mon Aug 24, 2009 6:08 pm
by jackpf
Hey, good job solving. I read this a while ago...but didn't have a clue so I left it.

Would you mind posting your remedy? It may help people who have a similar problem in the future? :)

Re: problems with mime mail

Posted: Mon Aug 24, 2009 6:56 pm
by arminium
No remedy

I ended up starting from scratch, and it works fine now, see below for working version and problems i found.....

The Working version below is much cleaner and much more re-usable, I might make time soon to make it a function, and post that here to so its easy for people to re-use ..... Its similar to something i found that didn't work, but i fixed and tidy'd it up. and hey presto we have a solution

still can't seem to find any reliably good info, most of what you can find is hit and miss to usability

I was comparing the source in emails in Thunderbird, outlook and windows mail and they looked fine.
problems i found
header line ends
\r\n breaks in Thunderbird for some reason
\n works with all
still having issues with return-path and you'll see its commented out in working verson, if you try to play with it don't forget to move the concatenation "." (for any newbies reading this that is the dot/period/fullstop what ever you want to call it, that is before the equal sign, which tells it that it adds the string to the existing string set in variable, the first one doesn't ever have it or php will return an error saying that the variable is not yet set)
looking over the old code, which i will mess with when i have more time there is a "\n" missing on last line of $header.....Not sure, but i only just noticed that few minutes ago, seems to output ok thou.....however .....its never as it seems....

this is the working set

Code: Select all

 
$to = ''; Put the email address you are sending to in here
$subject = ''; // put your subject in here
$msg = ''; // your plain text message goes in here
$stream = ''; // put your file from fopen() in here or file out put if generating on the fly
$filename ''; // put your file name here
 
$mime_boundary=md5(time());
//$headers  = 'Return-path: sales@company.com \n';
$headers  = 'From: company <no-reply@company.com>' . "\n";
$headers .= 'MIME-Version: 1.0'. "\n";
$headers .= "Reply-To: ".SITE_EMAIL."\r\n";
$headers .= 'bcc: Testbench <testbench@company.com>'."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"". "\n";
 
$msg  = "--".$mime_boundary. "\n";
$msg .= "Content-Type: text/plain; charset=iso-8859-1". "\n";
$msg .= "Content-Transfer-Encoding: 7bit". "\n\n";
$msg .= $textmessage . "\n\n";
 
$msg .= "--".$mime_boundary. "\n";
$msg .= "Content-Type: application/pdf; name=\"".$filename."\"". "\n";
$msg .= "Content-Transfer-Encoding: base64". "\n";
$msg .= "Content-Disposition: attachment; filename=\"".$filename."\"". "\n\n";
$msg .= chunk_split(base64_encode($stream)) . "\n\n";
$msg .= "--".$mime_boundary."--". "\n\n"; 
 
 
mail( $to, $subject, $msg, $headers );

Re: problems with mime mail

Posted: Mon Aug 24, 2009 7:00 pm
by jackpf
Nice one.

Just a thought, but your problems with return-path could be due to the email address not being quoted with gt and lt symbols (<>).

I had an issue with this when writing a mailer script...some SMTP clients are really strict, and won't accept an email address anywhere in the headers without <> brackets.

Re: problems with mime mail

Posted: Mon Aug 24, 2009 7:29 pm
by arminium
fixed the return path too

only 1/3 servers i just checked worked using

$headers = 'Return-path: <me@company.com>'. "\n";


the following worked on 3/3 servers (all have different config setup) sending to 3/3 email clients

mail( $to, $subject, $msg, $headers, "-f email@domain.com" );

NOTE: the space between the "-f" and the email address

Re: problems with mime mail

Posted: Mon Aug 24, 2009 7:33 pm
by jackpf
Oh right. I never knew that. Awesome :)

Re: problems with mime mail

Posted: Mon Aug 24, 2009 8:09 pm
by arminium
we all learnt some more today!!!