• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

Inlline attachments in HTML message


i want to send and email in html format...
i'm using the phpmailer class to send emails....

the html message contains inline attachments also... like
<img src="c:\windows\image.gif"> or <td background="c:\dir\image.jpg">

i need the user whom i'm sending this mail also to view the image properly.. what should i do to make it happen....

1 Solution
are those images not available on the web?
when i send html emails i reference all images to my website

<img src="http://www.mysite.com/images/image.gif">
sumachaaAuthor Commented:

it will definitely work if u give the web site adrs....

but what if I have an HTML file in my local system and i have to send this as my mail (not as an attachment...) and in that message we have different images which are local to my machine (not available thru a url)

When we sent a similiar message from Microsoft outlook.. they tackle this problem..... the local images are converted into some form like
<img src=cid:somevalue>

hope u got what i meant
Send yourself an e-mail in Microsoft Outlook with the image attached, then look at the raw contents of the message from the mailserver rather than looking at it through Outlook.  At least you'll be able to see how they do it.  Just beware that you may not be able to view the images on some other mail programs.  Best to do it using absolute URLs pointing to a server on the Internet - you're safer then.  Only downside is that Outlook 2003 will block these by default (although it tells the user so and gives them the option to download them).
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

i use this but i don't guarantee it will work on all mailers
i added a few comments for u (in english). they will be introduced by '[[['
beware, the '\n' and '\r' are VERY tricky and each mailer has its own tolerance and/or may reject some legal code
try the url few lines down and links to the corresponding rfc and mime syntax if you want to understand
i remember there were usefull pieces of code for simple mailing down the page of the mail() function in the php manual.
this should be very usefull to get the basic syntax.

the other way is use a mailer that handle images

this code works if $CID is set beforehand ( i use a custom print_image function for that )... see comments down

//création du mail
//headers + complets -> http://www.zend.com/zend/trick/html-email.php
$headers =
"From: webserver@host;
MIME-Version: 1.0;
Content-Type: multipart/related; boundary = \"$boundary\"
//pour les clients qui ne prennent pas en charge MIME
$msg = "ceci est un message au format MIME 1.0\n\n\r";

//en-tete html [[[ html headers
$msg .= "--$boundary\n";
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";      //charset inutile voire néfaste ?
$msg .= "Content-Transfer-Encoding:7bit
Content-Disposition: inline
Content-Base: \"YOURSERVER/\"\n\n";//il faut 7 bits pour la compat ASCI
//mail au format html (insérer du texte ici si besoin)
$msg.=$MAIL_html."</body></html>";// [[[ MAIL_html contains the html message with the 'cid' references
$msg .= "\n\n";
// [[[ $CID is an array (cid_refference => path_to_file)
//if (!$rep = @opendir($repimages))status('ERR_CREATION');
//while ($fichier = @readdir($rep)){//il faut lister les images existantes dans le mail
//      if($fichier='.' or $fichier='..')continue;
foreach($CID as $id => $fichier){$fichier="$repimages$fichier";
      $fp = fopen($fichier, "rb");
      $msg .= "--$boundary
Content-Type: image/jpeg; name=\"$fichier\""./*Content-Type: application/octet-stream*/"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
Content-ID: <$id>\n\n";
      $msg .= chunk_split(base64_encode(fread($fp, filesize($fichier))))."\n\n";
$msg .= "--$boundary--\n";

//---some code---

//envoi du mail + délai
if(($mode==2 and sleep(rand(1,5)))//simulation [[[ u don't need that
or @mail($emails, "PLANETECROISIERE",$MAIL_html,$headers) //mail serveur
or (ini_set("SMTP","smtp.wanadoo.fr") and mail($emails, "PLANETECROISIERE",$msg,$headers))){
// [[[ do whatever is needed when the mail is safely gone
thanks a lot both.

as i pointed out above,
<< beware, the '\n' and '\r' are VERY tricky and each mailer has its own tolerance and/or may reject some legal code >> (darn bad guy shamelessly quoting himself)

... the codes has actually been in use for some time, but i don't have that many contacts with the folks it was developped for... and there is not too many exotic mailers in use with their custommers.

as far as i know, this works flawlessly using outlook as a reader (quite extensive testings) ; there also have been a few tests run on netscape and opera mailers ; plaintext mailers will behave like they should do which meens a perfect mess as there is no alternate text version in this case...

that said, everybody is free to use, modify, paste improovements (a lot c/should be done)... and perhaps give some feedback as to the results in various mailers...

please don't SPAM.


one last thing. i forgot to paste the function for CID creation.

function affich_image($image,$bordure="",$alt="image from site whatever"){global $CID;while($im=@func_get_arg($i)){$i++;
                              if(is_array($attributs))foreach($attributs as $attribut=>$val)$attribs.=" $attribut=\"$val\"";
                              if(is_array($CID)){global $count_cid;$count_cid++;
                                    $CID[$count_cid]=$im;echo"<img src=\"cid:$aff\"";}
                              else echo"<img src=\"images/$im\" alt=\"$alt\" style=\"border : $bordure\"  $attribs>";return true;}}}

... takes an number of images names for input. (attributes may be added before and between images as array('attribute' => 'value', ...), priority given to the last array before the printed image.
... prints the html tag for the first availiable image in ;;/public/images (change as needed)
... store the image path in $CID instead if $CID is already declared as an array, and print the img tag subsequently

it seems there is something buggy in this version if the images were to be duplicated (check it out before you rely on this feature)

may also be a few ',' and quotes issues as i took out some of the code. (useless in this thraed + relied on other functions)

happy computing to all
I thought CID stand for something to do with the secret police in englands ?
hi, i've been a long time off the theads but this post may still be usefull to someone else

CID stands for content identifier and CIDs are the way to reference inline images in html mail.

see this excellent link http://mailformat.dan.info/headers/mime.html if you need clean and short but yet sufficiently detailed information.

<quote from the above link which i guess the author won't mind me to post>
The Content-ID header is primarily of use in multi-part messages (as discussed below); a Content-ID is a unique identifier for a message part, allowing it to be referred to (e.g., in IMG tags of an HTML message allowing the inline display of attached images). The content ID is contained within angle brackets in the Content-ID header.
</quote> .... see the site for more details and examples or use the above code.

see ya all

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now