New email library for Copernica

by Emiel Bruijntjes

We will soon replace the email library of Copernica Marketing Software. This library is responsible for both generating MIME messages (it turns an email object into a string) and for doing the opposite: to parse MIME strings into useful objects.

The library that is now in use has a long history. It started its life as a PERL module more than 10 years ago. Since then it has been ported to PHP, and although we have made enhancements to it, the core of the library is today still essentially the same as the engine that we built back then.

During the lifetime of the library many things have of course changed. PHP has become much more powerful with new concepts like more advanced features for object orientation, and with new built-in functions. Among the new functions in PHP are functions to parse and encode mime variables and bodies, and to run DNS queries - operations that we had all implemented ourselves in the old library, but that are of course much faster when implemented in PHP. This all made our library a little outdated and therefore we have decided to build a new library from scratch - which gave us the liberty to integrate it with our new MailerQ MTA at the same time.

Our target was to create a library that could be used for both generating and for parsing MIME messages, and do that both in exactly the same manner and with the same class. It should use built-in functions from PHP whenever possible to get the best performance. We also required that it would be fully object oriented, have support for attachments, embedded images and DKIM and that the parsing engine should automatically recognize disposition notifications and feedback reports from all popular email providers. And above all: the library should be very easy to use. We have achieved that.

Example how to use the new library to construct an email object
// construct an email object
$mail = new PxMail();
$mail->setHtml($htmlVersion);
$mail->setText($textVersion);
$mail->setTo(new PxtNamedEmailAddress('emiel.bruijntjes@copernica.com'));
$mail->setFrom(new PxtNamedEmailAddress('emiel.bruijntjes@copernica.com'));
$mail->setSubject('This is a sample subject');
$mail->sign($dkimkey);

// convert the email object to a MIME
echo(strval($mail));

Example how to use the library to parse a MIME message
// parse a mime object
$mail = PxMail::parse($mimestring);

// show some of the properties
echo "html: ".$mail->html()."\n\n";
echo "text: ".$mail->text()."\n\n";
echo "subject: ".$mail->subject()."\n\n";
foreach ($mail->to() as $address)
{
    echo("to: ".strval($mail->to())."\n");
}

This library is now ready, and we're very satisfied with the result. The code to build or parse an email now looks much prettier than before, and it is faster and more feature rich too. We have tested the new email engine with data from many different customers and with many different incoming example mails and it works better than our old library. It is that good that it is a waste not to make the library into a product or open source project in its own right - but that is something that I will address later.

Now the time has come to get rid of the old library and replace it completely with the new one. This is always exciting stuff because one can never know for sure what will happen if you put code in production - especially when you make changes to such core features as the Copernica email engine. What will happen to scheduled mailings that rely on a specific, maybe even wrong, behavior of the old library that is now fixed in the new lib? But we are confident that after all tests that we have performed over the last weeks, and after having used the library for specific beta users, it is going to be a great improvement.

Related articles

Copernica
email
PxMail
PHP
DKIM