Skip to content

Instantly share code, notes, and snippets.

@tsi
Created June 20, 2012 13:07
Show Gist options
  • Save tsi/2959814 to your computer and use it in GitHub Desktop.
Save tsi/2959814 to your computer and use it in GitHub Desktop.
HTML Mail Themer [Drupal 7]
<?php
/**
* Implements hook_mail_alter()
* Send mails to a file for easy debugging
*/
function mail_themer_mail_alter(&$message) {
$debug_dir = file_directory_temp() . '/mail_debug';
$debug_file = $debug_dir . '/mail_debug.html';
if (!is_dir($debug_dir)) {
if (!mkdir($debug_dir)) {
drupal_set_message(t('Mail message directory could not be created. Please consult your !watchdog for a detailed error description.', array('!watchdog' => l('log messages', 'admin/reports/dblog'))), 'error');
}
}
$output = '<!DOCTYPE html><meta charset="utf-8">';
$output .= '<p>';
$output .= '</br><strong>From:</strong> ' . $message['from'];
$output .= '</br><strong>To:</strong> ' . $message['to'];
$output .= '</br><strong>Subject:</strong> ' . $message['subject'];
$output .= '</p>';
if (is_array($message['body'])) {
foreach ($message['body'] as $item) {
$output .= $item;
}
} else {
$output .= $message['body'];
}
// Write debug information to a file
if (!file_put_contents($debug_file, $output)) {
drupal_set_message(t('Mail message could not be logged. Please consult your !watchdog for a detailed error description.', array('!watchdog' => l('log messages', 'admin/reports/dblog'))), 'error');
} else {
drupal_set_message(t('Mail message was logged to !debug_file', array('!debug_file' => $_SERVER['DOCUMENT_ROOT'] . $debug_file)), 'status');
}
// Write debug message to log
watchdog('mail_themer', $output, NULL, WATCHDOG_DEBUG, NULL);
drupal_set_message(t('Mail message was logged. Check out your !watchdog.', array('!watchdog' => l('log messages', 'admin/reports/dblog'))), 'status');
// display devel message
if (function_exists('kpr')) {
kpr($message);
}
// If TRUE, drupal_mail() will call drupal_mail_system()->mail() to deliver the message.
$message['send'] = FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment