How to send email with embedded images using zend framework?

The documentation says how to add inline binding, but what is the correct way to link to it from the html part? Is it possible to include images automatically, as in other libraries?

Maybe someone wrote a small fragment and is ready to share it?

+5
source share
2 answers

This is not a completely trivial thing, fortunately for you someone has subclassed Zend_Mail ( Demo_Zend_Mail_InlineImages) for this, see here:

http://davidnussio.wordpress.com/2008/09/21/inline-images-into-html-email-with-zend-framework/

+5
source

i write a wrapper for the mail class

private function _processHtmlBody($I_html, $I_mailer, $I_data) {

$html = $I_html;
$mail = $I_mailer;

$xmlBody = new DomDocument();
$xmlBody->loadHTML($html);

$imgs = $xmlBody->getElementsByTagName('img');
$I_data['atts'] = array();

$imgCount = 0;
foreach ($imgs as $img) {   
  $imgCount++;
  $imgUrlRel = $img->getAttribute('src');

  $imgId = sha1(time() . $imgCount . $imgUrlRel);
  $html = str_replace($imgUrlRel, 'cid:' . $imgId, $html);

  $imgUrlFull = 'http://' . $_SERVER['HTTP_HOST'] . $imgUrlRel;

  $imgBinary = file_get_contents($imgUrlFull);

  $imgPart = $mail->createAttachment($imgBinary);

  $imgPart->filename    = 'image' . $imgCount . '.jpg';
  $imgPart->id = $imgId;

  $I_data['atts'][] = $imgPart;
  }
  $mail->setBodyHtml($html);

  return $html;
}
+2

All Articles