jump to navigation

Sending emails Using Zend_Mail and Google Smtp February 8, 2013

Posted by Tournas Dimitrios in PHP.

mailboThe Zend Framework provides the Zend_Mail component to compose and send both text and MIME-compliant multipart e-mail messages . Due to ZF’s loosely coupled architecture , each component can be used as an supplementary part into other Frameworks or libraries . Mail can be sent with Zend_Mail’s default transport layer (Zend_Mail_Transport_Sendmail) or alternatively , using another transport layer  (Zend_Mail_Transport_Smtp) . This article will present a simple way to use the Smtp  layer for sending mails via our Google account . A custom Class encapsulates the basic functionality , this allows us to reuse the code in other projects (instead of copy/paste) . This Class depends on  ZF’s components , it is assumed that this Framework is already installed into your server . I always prefer to download the whole Framework , as its size is 35-MegaBytes (these days , it would be considered as negligible) .  You could of course only download  the needed modules (instead of the whole Framework) from this website .

Note: Google has smtp sending limits, you can only send up to 500 emails using a Google standard account. Read the Google answer on this limiting issue, for more info .

Straight to the code :

 * Created by Tournas Dimitrios
 * Date: 2/7/13
 * Time: 10:38 PM

class GoogleMailer
    protected $_parameters = array();

    public function __construct(array $mailComponents = array())
     $this->_parameters = $mailComponents;

    public function composeMail(array $mailComponents)
    $mailer = $this->_getMailer() ;
 list($fromAddress , $fromName) = explode(",", $mailComponents['fromMail']);
           ->setFrom($fromAddress , $fromName)
    if (array_key_exists('HtmlMessage', $mailComponents) &&
    $mailer->setBodyHtml($mailComponents['HtmlMessage']) ;
    if (array_key_exists('replyTo', $mailComponents) &&
 list($replyAddress , $replyName) = explode(",", $mailComponents['replyTo']);
    $mailer->setReplyTo($replyAddress , $replyName );
    return $mailer ;

    private  function _getMailer()
    $mailer = new Zend_Mail();
    return $mailer ;

    private  function _getSmtpTransport()
    return new Zend_Mail_Transport_Smtp('smtp.gmail.com', array(
            'auth'     => 'login',
            'username' => $this->_parameters['Google.username'],
            'password' => $this->_parameters['Google.password'],
            'ssl'      => 'ssl',
            'port'     => 465 ,

A quick overview  :

  • The constructor demands an array , which contains our Google credentials (Username , password)
  • Two private functions ( _getMailer  and _getSmtpTransport ) are handling the initialization process of the Classes .
  • A public function (composeMail) , is responsible for triggering the initialization of the two required Classes  and composing the mail object with our custom information ( From , Subject , Message , MailTo  , ReplyTo) . The  information is passed into this function as an associative array .
  • Besides the required parts of the mail message , optional fields can also be defined (HtmlMessage and replyTo ) . Two “if-statements” will recognize their existence and will take care to set the mail object .  Gmail automatically replaces whatever “setFrom”  or “setReplyTo” fields you have specified with the account you are authenticating with (yourname@gmail.com) . To define another “setReplyTo”  mail-address , you must allow Gmail to send using that email address by following these steps  .

The implementation :

 * Created by Tournas Dimitrios
 * Date: 2/7/13
 * Time: 11:16 PM
include 'GoogleMailer.php' ;
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$googleMail = new GoogleMailer(array(
    'Google.username' => 'YOUR-GOOGLE_USERNAME',
    'Google.password' => 'YOUR_GOOGLE_PASSWORD'));
$plainTextMessage = 'Lorem ipsum ....Vut integer et, nisi enim! Nisi lectus pid, nunc, eu aliquam aliquam sit montes cras. Aliquam mus non montes diam magnis placerat arcu augue, ' ;
$HtmlMessage = '<h3>Important message !!!</h3>' ;
$mailComponents = array(
    'plainTextMessage'=> $plainTextMessage ,
    'fromMail' => 'tournasdimitrios@gmail.com , Tournas Dimitrios ' ,
    'subject'  => 'Testing Zend_Mail with Google\'s SNMTP' ,
    'HtmlMessage' => $HtmlMessage . $plainTextMessage ,
    'replyTo'  => 'replytothisaddress@gmail.com , Tournas Dimitrios' ,
    'addTo' => 'sendtothisaddress@gmail.com');
     $mailer = $googleMail->composeMail($mailComponents)
                          ->send() ;
    echo 'Message send successfully' ;
    } catch(Exception $e) {
     echo $e->getMessage();

A quick overview :

  •  First we have to include our custom GoogleMailer Class
  • Zend Framework’s components must also be included . Instead of separate “include” statements for each component , I have chosen to instantiate ZF’s  autoloader Class .
  • A “try-catch” block will handle possible errors .
  • Everything else is self-explanatory (if not , drop me a comment) .


1. Inell Zdanowicz - February 14, 2013

Good day! Would you mind if I share your blog with my myspace group? There’s a lot of people that I think would really enjoy your content. Please let me know. Thanks

tournasdimitrios1 - February 14, 2013

Most (if not all) of my knowledge was gained from articles that were written by other Bloggers . In this context , every bit of my articles is based on their knowledge . Feel free to reproduce whole or parts of this Blog , under one condition , the content should not be sold and must freely accessible to all Internet users .

2. web page - April 17, 2014

Hi there! I simply would like to give you a big thumbs
up for the great info you have got here on this post.
I’ll be returning to your site for more soon.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s