maillog.mail.inc 4.2 KB

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