MtMail: e-mail module for ZF2

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' => 'johndoe@domain.com',
    'from' => 'contact@mywebsite.com',
    'subject' => 'Welcome!',
);
$message = $this->mtMail()->compose($headers, 'application/mail/welcome.phtml');
$this->mtMail()->send($message);

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',
        ),
        'layout' => 'application/mail/layout.phtml',
    ),
);
Layout template looks very similar to standard Zend\View layout:
<?php echo $this->content ?>



--
Kind regards,
Sales Team

Plugins

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.

Read more 

Using standalone Zend\View

Zend\View is pretty advanced rendering engine, with multiple useful features. It is working nicely within ZF2's MVC stack, where it is automatically configured for you. But how to use it without full MVC?

This can be useful in some situations: when building Your Own Microframework™, when creating an application based on ZF2 components, or (in my case) when working on module that is supposed to render something outside MVC flow. All of this projects can benefit from nested templates, multiple rendering engines, or pluggable architecture of Zend\View.

So, how to do that?

Read more 

Extracting single table from huge MySQL dump

During last few weeks I had to work with relatively big MySQL dumps. I had to find interesting rows in about 400 files, each of them taking 40 minutes to import. In order to speed things up, I found simple tool that allowed me to extract only interesting tables.

The tool is actually single Perl script, named extract_sql.pl (available on Github). It allows extracting tables with simple command:

mat@server:~$ extract_sql.pl -t TABLE_NAME -r DUMP_FILE.sql

This command will print dump to console output, so you may want to redirect it to some file:

mat@server:~$ extract_sql.pl -t TABLE_NAME -r DUMP_FILE.sql > table_name.sql

Finally, extract_sql.pl is able to read input from stdin, so it is easy to extract and import single table from compressed dump file:

mat@server:~$ zcat DUMP_FILE.sql | extract_sql.pl -t TABLE_NAME \
| mysql dest_database -u username -p
Read more 

Automated MySQL backup on dedicated server or VPS

I just moved all my small projects to new dedicated server. I have to admit, until now I wasn't paying attention to regular backups. I simply ran mysqldump and copied everything to my laptop every few months. I didn't have any problems with that, as my data was not very critical. But, this time I decided to build something better - I wanted database backups to be generated automatically, at regular intervals.

I knew more or less what to do, I just had to put all pieces together. This tutorial shows necessary steps to build similar solution on your server.

Read more 

Learning ZF2: Application flow

Zend Framework 2

This post covers basic tasks that you may want to do within controller: forwarding to different actions, redirecting, and displaying 404 page. Once again, I will show how this tasks can be achieved in both ZF1 and ZF2.

Read more 

Learning ZF2: The Controller

Zend Framework 2

When Zend Framework 2 was officially released, I wanted to learn it on practical example. So, I decided to use it as a foundation for my new blog engine. Because it is very simple application (just a few classes), I was able to build it pretty easily.

This is first post of short series where I will describe how common problems are solved in ZF2. I will always compare it with similar code written in ZF1-style.

First I'm going to describe some common tasks that you usually do in your controllers: handling input parameters and accessing application services.

Read more 

About me
Mateusz Tymek

After compiling my very first lines of code at the age of 12, I became passionate about computer science and technology. Now I'm a PHP developer, enjoying my work as a member of Cleeng team.
Doing some sports in my spare time.

Github activity