Page 1 of 1

How to find 'Content-ID' of an attachment

Posted: Tue Feb 03, 2009 10:31 am
by rickey80
Hi everyone,
I have coded an html email script in php. What I want to do is send an email with image embedded in the body. I can do it by using <img src = image path> method. But outlook will block those html email due to security risk. So I found a solution which first attach the image as an attachment and then reference that attachment into the html code using the Content-ID of the attachment. And use it in the img tag like this, <img src = cid:Content-ID>

My problem is I don't know how to find content-id of an attachment. So I guess one and use it as follows. But it's not working. If anyone can please support me in this regard and let me know where I have done wrong I will be very faithful.

<?php
$to = "myemail@yahoo.com"; //this is testing mail address
$subject = "Mime mails are very useful";
$from = "anymail@yahoo.com";

$fileattname = "/images/845104292.jpg";

$semi_rand = md5( time() );
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers = "From: $from";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$fileatt = "../images/845104292.jpg"; //here no need to use another variable for this. But for testing purposes I use it.

$file = fopen( $fileatt, 'rb' );
$data = fread( $file, filesize( $fileatt ) );
fclose( $file );



$message = "--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"\n<html><body><p>This is body of the message<img border=\"0\" src = \"cid:nnn@mime.mail\" width=\"99\" height=\"131\"></p></body></html>\n\n";

$data = chunk_split( base64_encode( $data ) );

$message .= "--{$mime_boundary}\n" .
"Content-Type: image/jpeg;\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"845104292.jpg\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n Content-ID: <nnn@mime.mail>" .
"--{$mime_boundary}--\n";

$qry = mail($to, $subject, $message, $headers);


if ($qry)
{
echo "EMail Sent";
}
else
{
echo "EMail Not sent";
}
?>

Thank you
Rickey