mandrill.mail.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @file
  4. * Implements Mandrill as a Drupal MailSystemInterface
  5. */
  6. /**
  7. * Modify the drupal mail system to use Mandrill when sending emails.
  8. */
  9. class MandrillMailSystem implements MailSystemInterface {
  10. /**
  11. * Concatenate and wrap the email body for either plain-text or HTML emails.
  12. *
  13. * @param array $message
  14. * A message array, as described in hook_mail_alter().
  15. *
  16. * @return array
  17. * The formatted $message.
  18. */
  19. public function format(array $message) {
  20. // Join the body array into one string.
  21. if (is_array($message['body'])) {
  22. $message['body'] = implode("\n\n", $message['body']);
  23. }
  24. return $message;
  25. }
  26. /**
  27. * Send the email message.
  28. *
  29. * @see drupal_mail()
  30. *
  31. * @param array $message
  32. * A message array, as described in hook_mail_alter().
  33. *
  34. * @return bool
  35. * TRUE if the mail was successfully accepted, otherwise FALSE.
  36. */
  37. public function mail(array $message) {
  38. // Optionally log mail keys not using Mandrill already. Helpful in
  39. // configuring Mandrill.
  40. if (variable_get('mandrill_log_defaulted_sends', FALSE)) {
  41. $systems = mailsystem_get();
  42. $registered = FALSE;
  43. foreach ($systems as $key => $system) {
  44. if ($message['id'] == $key) {
  45. $registered = TRUE;
  46. }
  47. if (!$registered) {
  48. watchdog(
  49. 'mandrill',
  50. "Module: %module Key: %key invoked Mandrill to send email because Mandrill is configured as the default mail system. Specify alternate configuration for this module & key in !mailsystem if this is not desirable.",
  51. array(
  52. '%module' => $message['module'],
  53. '%key' => $message['key'],
  54. '!mailsystem' => l(t('Mail System'), 'admin/config/system/mailsystem'),
  55. ),
  56. WATCHDOG_INFO
  57. );
  58. }
  59. }
  60. }
  61. $mailer = mandrill_get_api_object();
  62. // Apply input format to body.
  63. $format = variable_get('mandrill_filter_format', '');
  64. if (!empty($format)) {
  65. $message['body'] = check_markup($message['body'], $format);
  66. }
  67. // Extract an array of recipients.
  68. $to = mandrill_get_to($message['to']);
  69. // Prepare headers, defaulting the reply-to to the from address since
  70. // Mandrill needs the from address to be configured separately.
  71. // Note that only Reply-To and X-* headers are allowed.
  72. $headers = isset($message['headers']) ? $message['headers'] : array();
  73. if (!empty($message['from']) && empty($headers['Reply-To'])) {
  74. $headers['Reply-To'] = $message['from'];
  75. }
  76. // Prepare attachments.
  77. $attachments = array();
  78. if (isset($message['attachments']) && !empty($message['attachments'])) {
  79. foreach ($message['attachments'] as $attachment) {
  80. if (is_file($attachment)) {
  81. $attachments[] = $mailer->getAttachmentStruct($attachment);
  82. }
  83. }
  84. }
  85. // Determine if content should be available for this message.
  86. $blacklisted_keys = explode(',', mandrill_mail_key_blacklist());
  87. $view_content = TRUE;
  88. foreach ($blacklisted_keys as $key) {
  89. if ($message['id'] == drupal_strtolower(trim($key))) {
  90. $view_content = FALSE;
  91. break;
  92. }
  93. }
  94. // The Mime Mail module (mimemail) expects attachments as an array of file
  95. // arrays in $message['params']['attachments']. As many modules assume you
  96. // will be using Mime Mail to handle attachments, we need to parse this
  97. // array as well.
  98. if (isset($message['params']['attachments']) && !empty($message['params']['attachments'])) {
  99. foreach ($message['params']['attachments'] as $attachment) {
  100. $attachment_path = drupal_realpath($attachment['uri']);
  101. if (is_file($attachment_path)) {
  102. $struct = $mailer->getAttachmentStruct($attachment_path);
  103. // Allow for customised filenames.
  104. if (!empty($attachment['filename'])) {
  105. $struct['name'] = $attachment['filename'];
  106. }
  107. $attachments[] = $struct;
  108. }
  109. }
  110. // Remove the file objects from $message['params']['attachments'].
  111. // (This prevents double-attaching in the drupal_alter hook below.)
  112. unset($message['params']['attachments']);
  113. }
  114. // Account for the plaintext parameter provided by the mimemail module.
  115. $plain_text = empty($message['params']['plaintext']) ? drupal_html_to_text($message['body']) : $message['params']['plaintext'];
  116. // Get metadata.
  117. $metadata = isset($message['metadata']) ? $message['metadata'] : array();
  118. $from = mandrill_from();
  119. $mandrill_message = array(
  120. 'html' => $message['body'],
  121. 'text' => $plain_text,
  122. 'subject' => $message['subject'],
  123. 'from_email' => $from['email'],
  124. 'from_name' => $from['name'],
  125. 'to' => $to,
  126. 'headers' => $headers,
  127. 'track_opens' => variable_get('mandrill_track_opens', TRUE),
  128. 'track_clicks' => variable_get('mandrill_track_clicks', TRUE),
  129. // We're handling this with drupal_html_to_text().
  130. 'auto_text' => FALSE,
  131. 'url_strip_qs' => variable_get('mandrill_url_strip_qs', FALSE),
  132. 'bcc_address' => isset($message['bcc_email']) ? $message['bcc_email'] : NULL,
  133. 'tags' => array($message['id']),
  134. 'google_analytics_domains' => (variable_get('mandrill_analytics_domains', NULL)) ? explode(',', variable_get('mandrill_analytics_domains')) : array(),
  135. 'google_analytics_campaign' => variable_get('mandrill_analytics_campaign', ''),
  136. 'attachments' => $attachments,
  137. 'view_content_link' => $view_content,
  138. 'metadata' => $metadata,
  139. );
  140. $subaccount = variable_get('mandrill_subaccount', FALSE);
  141. if ($subaccount) {
  142. $mandrill_message['subaccount'] = $subaccount;
  143. }
  144. // Allow other modules to alter the Mandrill message, and sender/args.
  145. $mandrill_params = array(
  146. 'message' => $mandrill_message,
  147. 'function' => 'mandrill_sender_plain',
  148. 'args' => array(),
  149. );
  150. drupal_alter('mandrill_mail', $mandrill_params, $message);
  151. // Queue for processing during cron or send immediately.
  152. $status = NULL;
  153. if (mandrill_process_async()) {
  154. $queue = DrupalQueue::get(MANDRILL_QUEUE, TRUE);
  155. $queue->createItem($mandrill_params);
  156. if (variable_get('mandrill_batch_log_queued', TRUE)) {
  157. watchdog('mandrill', 'Message from %from to %to queued for delivery.',
  158. array(
  159. '%from' => $from['email'],
  160. '%to' => $to[0]['email'],
  161. ), WATCHDOG_NOTICE);
  162. }
  163. return TRUE;
  164. }
  165. else {
  166. return mandrill_mailsend($mandrill_params['message'], $mandrill_params['function'], $mandrill_params['args']);
  167. }
  168. }
  169. }