MailSystemInterface for logging mails to the filesystem.

To enable, save a variable in settings.php (or otherwise) whose value can be as simple as:

$conf['mail_system'] = array( 'default-system' => 'DevelMailLog', );

Saves to temporary://devel-mails dir by default. Can be changed using 'devel_debug_mail_directory' variable. Filename pattern controlled by 'devel_debug_mail_file_format' variable.

File

devel.mail.inc
View source
  1. /**
  2. * @file
  3. * MailSystemInterface for logging mails to the filesystem.
  4. *
  5. * To enable, save a variable in settings.php (or otherwise) whose value
  6. * can be as simple as:
  7. *
  8. * $conf['mail_system'] = array(
  9. * 'default-system' => 'DevelMailLog',
  10. *);
  11. *
  12. * Saves to temporary://devel-mails dir by default. Can be changed using
  13. * 'devel_debug_mail_directory' variable. Filename pattern controlled by
  14. * 'devel_debug_mail_file_format' variable.
  15. *
  16. */
  17. class DevelMailLog extends DefaultMailSystem {
  18. public function composeMessage($message) {
  19. $mimeheaders = array();
  20. $message['headers']['To'] = $message['to'];
  21. foreach ($message['headers'] as $name => $value) {
  22. $mimeheaders[] = $name . ': ' . mime_header_encode($value);
  23. }
  24. $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
  25. $output = join($line_endings, $mimeheaders) . $line_endings;
  26. // 'Subject:' is a mail header and should not be translated.
  27. $output .= 'Subject: ' . $message['subject'] . $line_endings;
  28. // Blank line to separate headers from body.
  29. $output .= $line_endings;
  30. $output .= preg_replace('@\r?\n@', $line_endings, $message['body']);
  31. return $output;
  32. }
  33. public function getFileName($message) {
  34. $output_directory = $this->getOutputDirectory();
  35. $this->makeOutputDirectory($output_directory);
  36. $output_file_format = variable_get('devel_debug_mail_file_format', '%to-%subject-%datetime.mail.txt');
  37. $tokens = array(
  38. '%to' => $message['to'],
  39. '%subject' => $message['subject'],
  40. '%datetime' => date('y-m-d_his'),
  41. );
  42. return $output_directory . '/' . $this->dirify(str_replace(array_keys($tokens), array_values($tokens), $output_file_format));
  43. }
  44. private function dirify($string) {
  45. return preg_replace('/[^a-zA-Z0-9_\-\.@]/', '_', $string);
  46. }
  47. /**
  48. * Save an e-mail message to a file, using Drupal variables and default settings.
  49. *
  50. * @see http://php.net/manual/en/function.mail.php
  51. * @see drupal_mail()
  52. *
  53. * @param $message
  54. * A message array, as described in hook_mail_alter().
  55. * @return
  56. * TRUE if the mail was successfully accepted, otherwise FALSE.
  57. */
  58. public function mail(array $message) {
  59. $output = $this->composeMessage($message);
  60. $output_file = $this->getFileName($message);
  61. return file_put_contents($output_file, $output);
  62. }
  63. protected function makeOutputDirectory($output_directory) {
  64. if (!file_prepare_directory($output_directory, FILE_CREATE_DIRECTORY)) {
  65. throw new Exception("Unable to continue sending mail, $output_directory is not writable");
  66. }
  67. }
  68. public function getOutputDirectory() {
  69. return variable_get('devel_debug_mail_directory', 'temporary://devel-mails');
  70. }
  71. }

Classes

Namesort descending Description
DevelMailLog