How to send an email with inline images using zend framework?

The documentation specifies how to add inline attachement, but what is the correct way of referencing it from the html part? Is it possible to include images automatically as it is in other libraries?

Maybe someone has written a little snippet and is willing to share?


That's not exactly a trivial thing, lucky for you someone has subclassed Zend_Mail (Demo_Zend_Mail_InlineImages) to do that, see here:

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

The solution for this issue is to embed the images into the email. It’s well explained on php.net, but probably it’ll take a precious time. Fortunately for the Zend Framework developers there is a faster way. I’ve implemented the code developed by David Nussio in the following model:


i write wrapper for 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;
}

Zend_Mail inline images in e-mail: CID attachment (tested in Gmail, Outlook.com, Outlook 2010) - Zend_Mail_InlineImages.php


Instead of extending Zend_Mail you can directly embed your image in base64 using html.

Here is an example image that I included in my email template:

<img src="data:image/jpg;base64,<?= base64_encode(file_get_contents("/local/path/to/image.png")) ?>" />

Files can be attached to an email using Zend_Mail->createAttachment () method. The createAttachment () method requires a content of the attachment, and 3 optional arguments. Please note that it takes file content as an argument, not the name of the file.


The createAttachment () method returns a Zend_Mime_Part object: $mail = new Zend_Mail (); $at = $mail -> createAttachment($myImage); $at -> type = 'image/gif'; $at -> disposition = Zend_Mime:: DISPOSITION_INLINE; $at -> encoding = Zend_Mime:: ENCODING_BASE64; $at -> filename = 'test.gif'; $mail -> send(); An alternative is to create an instance of Zend_Mime_Part and add it with addAttachment () :


 Attachments Files can be attached to an e-mail using the createAttachment() method. The default behaviour of Zend_Mail is to assume the attachment is a binary object (application/octet-stream), should be transferred with base64 encoding, and is handled as an attachment.


For Send Email Magento 2 without image attachment refer link, Send Mail from Custom module Magento 2. For Override TransportBuilder.php file in Magento 2, you need to create di.xml file. Using di.xml file you can override any PHP class in Magento 2.