maillog.mail.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * An interface for pluggable mail back-ends.
  4. */
  5. class MaillogMailSystem implements MailSystemInterface {
  6. /**
  7. * Format a message composed by drupal_mail() prior sending.
  8. *
  9. * @param $message
  10. * A message array, as described in hook_mail_alter().
  11. *
  12. * @return
  13. * The formatted $message.
  14. */
  15. public function format(array $message) {
  16. $default = new DefaultMailSystem();
  17. return $default->format($message);
  18. }
  19. /**
  20. * Send a message composed by drupal_mail().
  21. *
  22. * @param $message
  23. * Message array with at least the following elements:
  24. * - id: A unique identifier of the e-mail type. Examples: 'contact_user_copy',
  25. * 'user_password_reset'.
  26. * - to: The mail address or addresses where the message will be sent to.
  27. * The formatting of this string must comply with RFC 2822. Some examples:
  28. * - user@example.com
  29. * - user@example.com, anotheruser@example.com
  30. * - User <user@example.com>
  31. * - User <user@example.com>, Another User <anotheruser@example.com>
  32. * - subject: Subject of the e-mail to be sent. This must not contain any
  33. * newline characters, or the mail may not be sent properly.
  34. * - body: Message to be sent. Accepts both CRLF and LF line-endings.
  35. * E-mail bodies must be wrapped. You can use drupal_wrap_mail() for
  36. * smart plain text wrapping.
  37. * - headers: Associative array containing all additional mail headers not
  38. * defined by one of the other parameters. PHP's mail() looks for Cc
  39. * and Bcc headers and sends the mail to addresses in these headers too.
  40. *
  41. * @return
  42. * TRUE if the mail was successfully accepted for delivery, otherwise FALSE.
  43. */
  44. public function mail(array $message) {
  45. // Log the e-mail
  46. if (variable_get('maillog_log', TRUE)) {
  47. $record = new stdClass;
  48. // In case the subject/from/to is already encoded, decode with mime_header_decode
  49. $record->header_message_id = isset($message['headers']['Message-ID']) ? $message['headers']['Message-ID'] : NULL;
  50. $record->subject = $message['subject'];
  51. $record->subject = mime_header_decode($record->subject);
  52. $record->body = $message['body'];
  53. $record->header_from = isset($message['from']) ? $message['from'] : NULL;
  54. $record->header_from = mime_header_decode($record->header_from);
  55. $header_to = array();
  56. if (isset($message['to'])) {
  57. if (is_array($message['to'])) {
  58. foreach($message['to'] as $value) {
  59. $header_to[] = mime_header_decode($value);
  60. }
  61. }
  62. else {
  63. $header_to[] = mime_header_decode($message['to']);
  64. }
  65. }
  66. $record->header_to = implode(', ', $header_to);
  67. $record->header_reply_to = isset($message['headers']['Reply-To']) ? $message['headers']['Reply-To'] : '';
  68. $record->header_all = serialize($message['headers']);
  69. $record->sent_date = REQUEST_TIME;
  70. drupal_write_record('maillog', $record);
  71. }
  72. // Display the e-mail using Devel module
  73. if (variable_get('maillog_devel', TRUE) && function_exists('dpm')) {
  74. $devel_msg = array();
  75. $devel_msg[t('Subject')] = $message['subject'];
  76. $devel_msg[t('From')] = $message['from'];
  77. $devel_msg[t('To')] = $message['to'];
  78. $devel_msg[t('Reply-To')] = isset($message['reply_to']) ? $message['reply_to'] : NULL;
  79. $devel_msg[t('Header')] = $message['headers'];
  80. $devel_msg[t('Body')] = $message['body'];
  81. dpm($devel_msg, 'maillog');
  82. }
  83. if (variable_get('maillog_send', TRUE)) {
  84. $default = new DefaultMailSystem();
  85. $result = $default->mail($message);
  86. }
  87. elseif (user_access('administer maillog')) {
  88. $message = t('Sending of e-mail messages is disabled by Maillog module. Go <a href="@href">here</a> to enable.', array('@href' => url('admin/reports/maillog')));
  89. drupal_set_message($message, 'warning', TRUE);
  90. }
  91. else {
  92. global $user;
  93. watchdog('maillog', 'Attempted to send an email, but sending emails is disabled.');
  94. }
  95. return isset($result) ? $result : TRUE;
  96. }
  97. }