Zend Framework Source Code Snippets

Zend_Mail inline picture attachments

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
<?php
/**
 * Zend Framework
 *
 * LICENSE *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend * @package    Zend_Mail
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
  
/**
 * @see Zend_Mail
 */
require_once 'Zend/Mail.php'; 
/**
 * Class for sending an email.
 *
 * @category   Zend * @package    Zend_Mail
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Techi_Mail extends Zend_Mail{
 
        /**
     * Sets the HTML body for the message
     *     * @param  string    $html
     * @param  string    $charset
     * @param  string    $encoding
     * @return Zend_Mail Provides fluent interface
     */        public function setBodyHtml($html, $charset = null, $encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE, $preload_images = true)
        {
                if ($preload_images)
                {
                        $this->setType(Zend_Mime::MULTIPART_RELATED); 
                        $dom = new DOMDocument(null, $this->getCharset());
                        @$dom->loadHTML($html);
 
                        $images = $dom->getElementsByTagName('img'); 
                        for ($i = 0; $i < $images->length; $i++)
                        {
                                $img = $images->item($i);
                                $url = $img->getAttribute('src'); 
                                $image_http = new Zend_Http_Client($url);
                                $response = $image_http->request();
 
                                if ($response->getStatus() == 200)                                {
                                        $image_content = $response->getBody();
 
                                        $pathinfo = pathinfo($url);
                                        $mime_type = $response->getHeader('Content-Type'); 
                                        $mime = new Zend_Mime_Part($image_content);
                                        $mime->id          = $url;
                                        $mime->location    = $url;
                                        $mime->type        = $mime_type;                                        $mime->disposition = Zend_Mime::DISPOSITION_INLINE;
                                        $mime->encoding    = Zend_Mime::ENCODING_BASE64;
                                        $mime->filename    = $pathinfo['basename'];
 
                                        $this->addAttachment($mime);                                }
                        }
                }
 
                return parent::setBodyHtml($html, $charset, $encoding);        }
}

Comments

KingCrunch KingCrunch
2 years ago

Oh, really good idea ... So where is the reason to block images again? You guys are the reason, why I dont accept HTML-Mails anymore. :p

Techi Techi
2 years ago

I just do my job ;)

Baxter Baxter
1 year ago

Viewing these inline images in Uebimiau Webmail throws an preg_replace error as it doesn't sanitize the content-id before passing it to the preg_replace. To fix I've changed the following line:

$mime = new Zend_Mime_Part($image_content); $mime->id = $url;

to

$mime = new Zend_Mime_Part($image_content);
$mime->id = md5($url);

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

This simple code will load pictures from HTML email and create special attachment, which will force to display pictures in email clients even if they block pictures

Snippet details

Created:
Techi Techi
2 years ago
Edited:
Techi Techi
2 years ago
Revision Id:
120
Edit Message:
Initial Release
ZF Version
1.6.2
Tags:
Zend_Mail
Comments:
3
Views:
3781
Points:
1 (1 votes)

History

r120

Initial Release

Techi Techi
2 years ago
diff
r119

Initial Release

Techi Techi
2 years ago