jump to navigation

Sending HTML-email with Attachment Using Zend Framework’s Mail component November 18, 2011

Posted by Tournas Dimitrios in PHP.

My previous article ” Sending HTML-email with Attachment Using PHP’s mail() Function ” has shown that it’s possible to configure the headers of an email-message for embedding all kind of content into the message body . That article is an excellent introduction for understanding the structure of an  email-body , but for production PHP-code we can use more simpler methods to achieve the same results . This article will present Zend Framework’s mail component .

ZF is a fully object-oriented framework , and as such ,  it utilizes a lot of object-oriented (OO) concepts like inheritance and interfaces . By design , Zend Framework is simply a collection of objects (classes) .The Framework has ca 3000 files with over 800 components , from Authentication to AMF request/response functionality . These build in components can help you  develop any kind of functionality into your PHP project , experienced developers can even extend these components to create custom functionality . Normally , you’ll use Zend to create a fully functional MVC ZF- project that implements Enterprise level functionality , but  you can just load the components you need in simple PHP projects . ZF is very decoupled , which means we can take advantage of the components as individual libraries , instead of the framework as a whole .

require_once 'Zend/Mail.php' ;
require_once 'Zend/Mime/Part.php' ;
require_once 'Zend/Mime.php' ;

// Instatiate a new message object
$message = new Zend_Mail('utf-8');
$htmlText = "
  <title>Report of last months log files</title>
<span style="color: red;"><strong>Please keep in mind :</strong></span> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
</html> " ;
//Use the fluent interface to set message properties
$message->setFrom('administrator@mail.com' , 'John Doe' )
->setSubject('Testing Zend-Mail')
->setBodyText("And this plain text ")
->addHeader('Importance' , 'high')
->addTo('user134@mail.com' , 'Tournas Dimitrios')
->addCc('other.huy@example.com', 'Other Guy')
->addBcc('t.person@example.com' , 'The Third Person');
// path to a file
$file = 'picture.jpg';
$at = new Zend_Mime_Part(file_get_contents($file));
$at->filename = basename($file);
$at->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$at->encoding = Zend_Mime::ENCODING_BASE64;
$send = $message->send();
if ($send) {
echo "</pre></pre>
<h1>Weldone dimitrios</h1>
}else {
echo "<h1> No message where send </h1>" ;


This code is much “cleaner” as the code of “Sending HTML-email with Attachment Using PHP’s mail() Function ” . Zend’s “fluent interface” technique creates much simpler code . I have skipped many details as they are mentioned in the pre-mentioned article . Oh  … and you should already know how to upload the ZF-framework to your server .

Read more :


1. Sending HTML-Mail With Attachement Using PHP’s PEAR::Mail Package « Tournas Dimitrios - January 3, 2012

[…] Sending HTML-email with Attachment Using Zend Framework’s Mail component […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s