Building a Simple Sent Email Viewer in Drupal 7

This is useful for a client that doesn’t want to use an email service such as MailChimp, or Constant Contact. I have to create a simple bulk mailing capability in Drupal 7 with a searchable archive of sent messages.

Some existing solutions exist, but I don’t think they fit my needs:

  • Mail Logger: the UI is hard coded to a path, eg it doesn’t use Views and looks cumbersome to tweak.

  • Mail Statistics [Sandbox project]: this looks GREAT, but it assumes emails are sent to Drupal email addresses and not the general public. I may revisit this project.

  • MailLog or Devel Mail: I want searchable archived mail if at all possible; and don’t want to rely on Devel for this.

Solution: Message module + hook_mail_alter + Views

Since the above did not meet my needs. I have tentatively rolled my own solution. The key to my solution currently is the Message module and the Views Send module. Which provides essentially an activity stream or log type of Entity — and a mechanism to bulk send emails.

I created a new Message Type and defined some typical email fields to log per sent email. I make no use of the default message body supplied by Message — I’m simply defining custom email based fields as needed. Currently I track: to, subject, body and sent datetime.

Custom Message Type with additional attached Fields.

Custom Message Type with additional attached Fields.

I can then use hook_mail_alter in a custom module to interact with any outgoing email. We disable or redirect all email to a test account, and log outgoing emails using the Message module:

/**
 * Implements hook_mail_alter.
 *
 * Avoid sending email out in -dev mode.
 * Log outgoing email from views_send module used for bulk emailing.
 */
function MYMODULE_mail_alter(&$message) {
 $DEVELOPMENT_MODE = 1;
 if ($DEVELOPMENT_MODE) {
    // set 'To' field to nothing to disable outgoing email.
    $message['to'] = test@id.ucsb.edu';
 }
 if ($message['id'] == 'views_send_direct') {
   _MYMODULE_log_email_attempt($message);
 }
}

Lastly we log our email using a custom module function:

/**
 * Log an outgoing email attempt using the Message contrib module. A related
 * View offers a searchable table of "Sent" emails.
 */
function _MYMODULE_log_email_attempt($msg) {
 $system_user = user_load_by_name('INTERNALSystemUser');
 $message = message_create('MYMODULE_email_log_entry', array(), $system_user);
 $type = 'message';
 $bundle = 'MYMODULE_email_log_entry';
 $wrapper = entity_metadata_wrapper($type, $message);
 $wrapper->field_ssemaillog_to->set($msg['to']);
 $wrapper->field_ssemaillog_subject->set($msg['subject']);
 $wrapper->field_ssemaillog_body->set($msg['body'][0]);
 $wrapper->field_ssemaillog_sent_at->set(
    _MYMODULE_convert_timestamp_to_mysql_datetime()
 );
 $wrapper->save();
}

/**
 * Return a timestamp for a datetime field. Optionally do it to per-second
 * accuracy or per-page-load accuracy; this is a small performance tweak.
 */
function _MYMODULE_convert_timestamp_to_mysql_datetime() {
 $email_logging_lite = TRUE;
 $ts = ($email_logging_lite) ? REQUEST_TIME : time();
 return $ts;
}

All this code effectively logs all outgoing email as Message (log) entries from the Views Send module. Of note is the use of entity_metadata_wrapper to easily create a new Entity, and message_create from the Message module to create a new blank message instance.

A simple view then allows us to browse sent email:

A generic View to browse emails with some custom filters.

A generic View to browse emails with some custom filters.

Some niceties you can add into your Views, or its layout can be accomplished with Display Suite (extras) and Views Better Exposed Filters and the Date UI module.

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *


6 × 3 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>