devel.mail.inc

  1. nittany7 modules/contrib/devel/devel.mail.inc
  2. cis7 modules/contrib/devel/devel.mail.inc
  3. mooc7 modules/contrib/devel/devel.mail.inc

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.

Classes

Namesort descending Description
DevelMailLog

File

modules/contrib/devel/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. $output .= $message['subject'] . $line_endings;
  27. $output .= preg_replace('@\r?\n@', $line_endings, $message['body']);
  28. return $output;
  29. }
  30. public function getFileName($message) {
  31. $output_directory = $this->getOutputDirectory();
  32. $this->makeOutputDirectory($output_directory);
  33. $output_file_format = variable_get('devel_debug_mail_file_format', '%to-%subject-%datetime.mail.txt');
  34. $tokens = array(
  35. '%to' => $message['to'],
  36. '%subject' => $message['subject'],
  37. '%datetime' => date('y-m-d_his'),
  38. );
  39. return $output_directory . '/' . $this->dirify(str_replace(array_keys($tokens), array_values($tokens), $output_file_format));
  40. }
  41. private function dirify($string) {
  42. return preg_replace('/[^a-zA-Z0-9_\-\.@]/', '_', $string);
  43. }
  44. /**
  45. * Save an e-mail message to a file, using Drupal variables and default settings.
  46. *
  47. * @see http://php.net/manual/en/function.mail.php
  48. * @see drupal_mail()
  49. *
  50. * @param $message
  51. * A message array, as described in hook_mail_alter().
  52. * @return
  53. * TRUE if the mail was successfully accepted, otherwise FALSE.
  54. */
  55. public function mail(array $message) {
  56. $output = $this->composeMessage($message);
  57. $output_file = $this->getFileName($message);
  58. return file_put_contents($output_file, $output);
  59. }
  60. protected function makeOutputDirectory($output_directory) {
  61. if (!file_prepare_directory($output_directory, FILE_CREATE_DIRECTORY)) {
  62. throw new Exception("Unable to continue sending mail, $output_directory is not writable");
  63. }
  64. }
  65. public function getOutputDirectory() {
  66. return variable_get('devel_debug_mail_directory', 'temporary://devel-mails');
  67. }
  68. }