devel.mail.inc 2.7 KB

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