Forums

Embedded Email Images

thmix 08 May, 2010
Is there a way to embed an image in an email using a MIME Content-Type of multipart/related (RFC 2837)? The image would then get pulled into the HTML email using the cid: scheme. Even though the email will be larger, the image will not get blocked by the email client waiting for content approval when it is being pulled from a server.
GreyHead 09 May, 2010
Hi thmix,

I've never seen this done - it may be possible but I don't know how.

Bob
thmix 09 May, 2010
Bob,

Thanks for the quick reply. For now I'll just pull the image from the server. There were some good posts on how to do this in the forums which made it easy. There are several PHP code examples I found to send an embedded image in an email that I might try to implement in the future when I have more time. I included one of them for reference. This example also sends an embedded attachment.


<?php
 // Setting a timezone, mail() uses this.
 date_default_timezone_set('America/New_York');
  // recipients
 $to  = "you@phpeveryday.com" . ", " ; // note the comma 
 $to .= "we@phpeveryday.com"; 

  // subject 
 $subject = "Test for Embedded Image & Attachement"; 

 // Create a boundary string.  It needs to be unique 
 $sep = sha1(date('r', time()));

 // Add in our content boundary, and mime type specification:  
 $headers .=
    "\r\nContent-Type: multipart/mixed; 
     boundary=\"PHP-mixed-{$sep}\"";

 // Read in our file attachment
 $attachment = file_get_contents('attachment.zip');
 $encoded = base64_encode($attachment);
 $attached = chunk_split($encoded);

 // additional headers
 $headers .= "To: You <you@phpeveryday.com>, 
             We <we@phpeveryday.com>\r\n"; 
 $headers .= "From: Me \r\n"; 
 $headers .= "Cc: he@phpeveryday.com\r\n"; 
 $headers .= "Bcc: she@phpeveryday.com\r\n";

 $inline = chunk_split(base64_encode(
           file_get_contents('mypicture.gif')));

 // Your message here:
 $body =<<<EOBODY
 --PHP-mixed-{$sep}
 Content-Type: multipart/alternative; 
               boundary="PHP-alt-{$sep}"

 --PHP-alt-{$sep}
 Content-Type: text/plain

 Hai, It's me!


 --PHP-alt-{$sep}
 Content-Type: multipart/related; boundary="PHP-related-{$sep}"

 --PHP-alt-{$sep}
 Content-Type: text/html

 <html>
 <head>
 <title>Test HTML Mail</title>
 </head>
 <body>
 <font color='red'>Hai, it is me!</font>
 Here is my picture: 
  <img src="cid:PHP-CID-{$sep}" />
 </body>
 </html>
 
 --PHP-related-{$sep}
 Content-Type: image/gif
 Content-Transfer-Encoding: base64
 Content-ID: <PHP-CID-{$sep}> 
 
 {$inline}
 --PHP-related-{$sep}--
 
 --PHP-alt-{$sep}--

 --PHP-mixed-{$sep}
 Content-Type: application/zip; name="attachment.zip"
 Content-Transfer-Encoding: base64
 Content-Disposition: attachment

 {$attached}

 --PHP-mixed-{$sep}--
 EOBODY;
 
 // Finally, send the email
 mail($to, $subject, $body, $headers);
 ?>


thmix
This topic is locked and no more replies can be posted.