MtMail: e-mail module for ZF2

by Mateusz Tymek — on Zend Framework, PHP

Head's up! This post was written back in 2014 and is very likely to contain outdated information.

I'm happy to present a ZF2 module that handles composing and sending e-mail messages.

Why another module? There are a few of them already available on ZF modules website. However, when I was looking for solution to use in my application, I quickly realized that most of them are either outdated, or they miss features I needed. That's why I decided to write my own.

My intention was to create something powerful, but still simple to use. You an customize e-mail headers, add layout, automatically generate plaintext version of HTML e-mail, and so on. But you can also start composing and sending e-emails from your controllers with just a few lines of code.

Here's an example:

You start with configuring mail transport:

return array(
    'mt_mail' => array(
        'transport' => 'Zend\Mail\Transport\Smtp',
        'transport_options' => array(
            'host' => 'HOST',
            'connection_class' => 'plain',
            'connection_config' => array(
                'username' => 'USER_NAME',
                'password' => 'PASSWORD',
                'ssl' => 'tls',

That's all - now you can send your first e-mail!

$headers = array(
    'to' => '[email protected]',
    'from' => '[email protected]',
    'subject' => 'Welcome!',
$message = $this->mtMail()->compose($headers, 'application/mail/welcome.phtml');

Executed from controller, this will render welcome.phtml template, and send it as an e-mail message. MtMail uses Zend\View for rendering, so you can use all your view helpers, child templates, and so on.

After you have this simple code working inside your application, you can incrementally update your configuration to benefit from another MtMail features. For instance, you can set common layout template:

return array(
    'mt_mail' => array(
        'composer_plugins' => array(
        'layout' => 'application/mail/layout.phtml',
Layout template looks very similar to standard Zend\View layout:
<?php echo $this->content ?>

Kind regards,
Sales Team


Finally: do you miss any feature, or want to do something very specyfic with your e-mail? You can easily add it by writing a plugin. Currently following plugins are available:

  • Default headers - use it to share common headers across all your messags (useful for setting default From: and ReplyTo: headers)
  • Layout - give common layout to all your messages
  • MessageEncoding - use given charset to encode e-mail headers
  • PlaintextMessage - automatically generates text version of HTML message

See documentation.

Release Plan

MtMail is still under development.
Right now I'd like to get some opinions on how events are triggered (I think it could be a bit more elegant). Depending on how it goes, I will incorporate this feedback, which may result in small BC break. Then I'm going to tag version 1.0.

comments powered by Disqus