Logging Mails send via Zend_Mail with Zend_Log using a Mail-Transport
Disclaimer: This article is old but was not yet publicated here, but probably somebody can still use those informations
For my blog I wanted a simple way to log all Mails send via Zend_Mail to a File or Database, as fallback if the mailserver went down and for having all contact-mails accessable in the administration-backend too. The possible Ideas I first had were:
- Change all the code where I send a mail and add logging by hand with Zend_Log
- Extend Zend_Mail with a setLog and overwrite the send-Method
- Write a Transport for Logging
First way was out very fast, because it was ugly, ugly, ugly ... and would involve changing a lot of Code and repeating myself (DRY), nothing I really wanted to.
While looking at the other two options I choose the Logging-Transport-version, which meant to write some kind of proxy for the transport too because Zend_Mail only supports one
transport at a time and I did not want to only have logging.
So I wrote Robo47_Mail_Transport_Multi and Robo47_Mail_Transport_Log.
Robo47_Mail_Transport_Log supports setting the used priority and using a category for logging, like my extended version of Zend_Log (Robo47_Log) and most other classes I have written which allow logging (Robo47_ErrorHandler, Robo47_Controller_Plugin_Tidy). The Log-Transport supports formatters for the Zend_Mail-Object, I wrote 2 Formatters, a simple one which justs greps Subject, recipients and text/html-content concats it into a string and logs it and a second one which just uses serialize on the Zend_Mail-object.
You liked it ? Link it on your homepage or blog: