Wednesday, November 19, 2008

Sending multipart MIME mail with inline image

From today morning itself, I'm trying to send a MIME mail with PHP's mail() function that displays an image inline. But, I don't know where the <bleep> I'm making the mistake. The image goes but in GMail it shows up as an attachment instead of getting displayed inline. Here's my source code:
<?php
$to="prab97@indiatimes.com";
$from="help@indiatimes.com";
$subject="Verify";
$boundary="whatever";
$headers="MIME-Version: 1.0\r\n";
$headers.="Content-type: multipart/mixed; 
      boundary=\"$boundary\"\r\n";
$headers.="From: ".$from."\r\n";
$headers.="Reply-To: ".$from."\r\n";
$message = "--$boundary\n";
$message.="Content-type: image/png\n";
$message.="Content-Transfer-Encoding: base64\n";
$message.="Content-disposition: inline\n\n";
$encoded = base64_encode(file_get_contents("image.png"));
$message.=$encoded."\n";
$message.="--$boundary\n";
$message.="Content-type: text/html; charset=\"iso-8859-1\"\n";
$message.="Content-Transfer-Encoding: 7bit\n";
$message.="Content-disposition: inline\n\n";
$message.="This is a test. <a href="test.tst">Test</a>\n";
$message.="--$boundary--";
$send=mail($to, $subject, $message, $headers);
if($send){
echo "Mail was sent successfully!";
}
else {
echo "Failed!!!";
}
?>


I tried multipart/mixed, multipart/alternative, multipart/related but can't succeed in making it display inline. I want the image to be displayed first, followed by html text. There's no clue I'm getting about where I went wrong. If you've any idea then do tell me.

2 comments:

Sidney Rees said...

I am having the same problem, did you find a solution? Can you help me?

My mail is sidneyrees@gmail.com

Thank you in advanse,

Sid

prab97 said...

Not yet. Actually I didn't poke my head into it again.