如何使用zend框架发送带有嵌入式图像的电子邮件?

问题描述 投票:5回答:3

文档指定了如何添加内联附件,但是从html部分引用它的正确方法是什么?是否可以像在其他库中一样自动包含图像?

也许有人写了一些摘录并愿意分享?

php zend-framework email inline-images
3个回答
6
投票

这不完全是一件琐碎的事,幸运的是,有人已经将Zend_Mail(Demo_Zend_Mail_InlineImages)子类化了,所以请参见这里:

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


2
投票

我为邮件类编写包装器

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;
}

0
投票

代替扩展Zend_Mail,您可以使用html将图像直接嵌入base64中。

这是我包含在电子邮件模板中的示例图像:

<img src="data:image/jpg;base64,<?= base64_encode(file_get_contents("/local/path/to/image.png")) ?>" />
© www.soinside.com 2019 - 2024. All rights reserved.