PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Sending Mail

Note: This article was originally published at Planet PHP on 22 May 2012.
Planet PHP

Like every product, Natural Load Testing needs to send some mail, having written and re-written a bunch of different blocks of mail code over the years, I wanted to come up with something that would work now, and continue to serve us well into the future.

Basic Requirements:

  • Send multipart/mime mail, so the emails can look aomore officiala than plain text for the average customer. Users reading mail in plain text still get something nice.
  • Easy to send a custom email with a block or two of text in that format. This way we can leverage our system to send system notifications and updates, without any additional coding.
  • Easy to customize templates for automated messages like new account sign ups, expiry, and the like.

Last night I went back to use the code for the first time in months, always the true test of code, and it worked well!

How it works

I'm using the Zend_Mail class within my Lithium based application to send the multipart/mime emails. Writing my own mime handler would have been foolish, and it would have been wrong. It's a great class, and I was happy to leverage it. Integrating it with Lithium ended up being a bit trickier than I was expecting, my class in /libraries starts like this:

namespace app\libraries;use lithium\core\Libraries; use Zend_Mail; use Zend_Mail_Transport_Sendmail; Libraries::add(aoZenda, array(aoprefixa = aoZend_a, aopatha = ao/var/www/phpLibraries/Zenda, aobootstrapa = aoLoader/Autoloader.phpa, aoloadera = array(aoZend_Loader_Autoloadera, aoautoloada), aotransforma = function($class) { return str_replace(ao_a, ao/a, $class) . ao.phpa; }));

Past that the class itself is also very brief:

class wlmailer{ public $mail, $htmlMail, $plainMail, $options; public function __construct() { require_once('Zend/Mail.php'); $this-mail = new Zend_Mail(); $tr = new Zend_Mail_Transport_Sendmail('-fsupport@wondernetwork.com'); Zend_Mail::setDefaultTransport($tr); $this-mail = new Zend_Mail('utf-8'); } public function basic($options) { $template = aobeta.htmla; extract($options); $mail = include('a/app/libraries/mailTemplates/' . $template); $this-htmlMail = $mail['html']; $this-plainMail = $mail['plain']; $this-options = $options; } public function send() { $this-mail-setBodyText($this-plainMail, aUTF-8'); $this-mail-setBodyHtml($this-htmlMail, aUTF-8'); $this-mail-setFrom('support@wondernetwork.com', aNatural Load Testing'); $this-mail-addTo($this-options['to'], $this-options['name']); $this-mail-addBcc(aopaul@wondernetwork.coma, aoPaul Reinheimera); $this-mail-setSubject($this-options['subject']); $this-mail-send(); } }

Upon construction the class creates an instance of Zend_Mail, configures it to set the appropriate return path using the -f switch, and selects UTF-8 for the character set for outgoing mail.

I called the next method basic as I'd initially thought I'd end up with several classes, but it ended up being all I needed. It accepts parameters in the $options array, and uses extract() to pull all the key value pairs in the array into the current scope. That done it includes the indicated template (defaulting to abeta.html') which uses various tokens like $name, $email, $password etc to construct the message, and defines two copies of the message, a plain text and an html version. Those message versions are saved in public properties, so the calling class could manipulate them as required.

I'm leveraging the html Email Boilerplate template in my html emails, it's fantastic. The format of the included template is something like this:

I

Truncated by Planet PHP, read more at the original (another 1728 bytes)