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

3

6

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?

Sherlocke answered 6/7, 2009 at 16:30 Comment(0)
P
6

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/

Proliferous answered 6/7, 2009 at 16:56 Comment(1)
Here's another example: gist.github.com/3415630 (The link above is only for local file:/// images)Undertrick
C
2

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;
}
Chalkstone answered 14/7, 2009 at 12:43 Comment(0)
P
0

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")) ?>" />
Pirate answered 30/10, 2019 at 16:45 Comment(2)
That wont work. Gmail doesn't show inline images unfortunatelyMarch
@March try it because I'm using it for my production app and it works greatPirate

© 2022 - 2024 — McMap. All rights reserved.