Testing Zend_Mail

February 14th, 2010 Adrian Leave a comment Go to comments

Since I work on a local development machine/server, I’ve never taken the time to set up mail yet, nor do I want to. I think a staging environment is more appropriate to actually have email being sent out. Nevertheless, it has made testing any email functionality a little cumbersome. I’ve done a little research, and have found two ways to tackle the problem. I’ve also included code samples and other resources to get you started.

Create a new mail transport class

Zend has the ability to set a default mail transport class to be used when none is specified, which I’d imagine to be 99% of the time. To set it, all you need to do is put this somewhere early in your application before any mail code is executed.

Zend_Mail::setDefaultTransport(new Site_Mail_Transport_Debug());

The alternative (for the 1% who want to manually specify), you’d simply pass an instance of your mail transport object to Zend_Mail::send() when you call it. What should your class contain? It has to extend Zend_Mail_Transport_Abstract, which at this time of writing this, will specifically need to override the abstract _sendMail() method.

You can use it to create a new record in your database, create a file, or do whatever you need it to. I quickly hacked together one to log it to the database, and based it off of the Sendmail transporter, which is pretty much a wrapper for PHP’s mail function. Here is the code I came up with for those who want some direction. It’s very basic, but should be enough to get you started.

<?php

class Site_Mail_Transport_Debug extends Zend_Mail_Transport_Abstract {

    public function __construct(Zend_Db_Adapter_Abstract $db) {
        $this->_db = $db;
    }

    public function _sendMail() {
        $this->_db->insert(
            'mailtest',
            array(
                'recipients' => $this->recipients,
                'subject'    => $this->_mail->getSubject(),
                'body'       => $this->body,
                'header'     => $this->header
            )
        );
    }
}

Override it at the server level

This is a good idea if you don’t use Zend Framework, or don’t use it for all of your applications. There are two great techniques described on akrabat’s blog. Here they are.

The first, is to override php.ini’s ’sendmail_path’ and create your own script to handle it. If you were to set it to

sendmail_path = /usr/local/bin/trapmail

then you’d use the following code

formail -R cc X-original-cc
  -R to X-original-to
  -R bcc X-original-bcc
  -f -A"To: your@emailaddress.com"
| /usr/sbin/sendmail -t -i

This requires you to have mail configured on your server, but it still solves the underlying problem of wanting to debug the emails, and also not send out test emails to real users in your application.

The second solution provided is platform independent is for Windows users, and it’s to install fakemail. This program acts as an outgoing mail server, but instead creates files containing any mail passed to it.

Hopefully this helps others out who are also frustrated with dealing with pesky email debugging.

  1. February 15th, 2010 at 09:38 | #1

    hi

    why u did that? (setDefaultTransport) i mean

  2. Adrian
    February 15th, 2010 at 09:54 | #2

    That will make it so any outgoing mail sent afterwards is sent (or captured) using the object you pass to it. This is so you do not have to alter all of your application code and pass your custom transport object to every send() method.

    The custom transport object in this case does not actually send any email, but instead just captures it to your local database.

  3. Petar
    February 16th, 2010 at 01:16 | #3

    Hi, actually fakemail is python or perl script, so it works on any platform that supports these two, not only Windows.

  4. Adrian
    February 16th, 2010 at 10:00 | #4

    Thanks for the clarification. I’m assuming the original author meant it as a catch-all / backup plan for those who cannot easily do the sendmail modification.

  5. February 28th, 2010 at 09:38 | #5

    Excelente Articulo, Gracias

  1. February 18th, 2010 at 08:12 | #1
  2. March 13th, 2010 at 06:54 | #2