mailgun.mail.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @file
  4. * Implements Mailgun as a Drupal MailSystemInterface.
  5. */
  6. /**
  7. * Modify the Drupal mail system to use Mailgun when sending e-mails.
  8. */
  9. class MailgunMailSystem implements MailSystemInterface {
  10. /**
  11. * Concatenate and wrap the e-mail body for either plain-text or HTML e-mails.
  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. // Run the message through text format if specified in Mailgun settings.
  25. $format = variable_get('mailgun_format', '_none');
  26. if ($format !== '_none') {
  27. $message['body'] = check_markup($message['body'], $format);
  28. }
  29. // Wrap body with theme function.
  30. $message['body'] = theme('mailgun_message', array(
  31. 'subject' => $message['subject'],
  32. 'body' => $message['body'],
  33. 'message' => $message,
  34. ));
  35. return $message;
  36. }
  37. /**
  38. * Send the e-mail message.
  39. *
  40. * @param array $message
  41. * A message array, as described in hook_mail_alter(). $message['params']
  42. * may contain additional parameters. See mailgun_send().
  43. *
  44. * @return bool
  45. * TRUE if the mail was successfully accepted or queued, FALSE otherwise.
  46. *
  47. * @see drupal_mail()
  48. * @see https://documentation.mailgun.com/api-sending.html#sending
  49. */
  50. public function mail(array $message) {
  51. // Build the Mailgun message array.
  52. $mailgun_message = array(
  53. 'from' => $message['from'],
  54. 'to' => $message['to'],
  55. 'subject' => $message['subject'],
  56. 'text' => drupal_html_to_text($message['body']),
  57. 'html' => $message['body'],
  58. );
  59. // Add CC, BCC and Reply-To fields if not empty.
  60. $headers = array_change_key_case($message['headers']);
  61. if (!empty($headers['cc'])) {
  62. $mailgun_message['cc'] = $headers['cc'];
  63. }
  64. if (!empty($headers['bcc'])) {
  65. $mailgun_message['bcc'] = $headers['bcc'];
  66. }
  67. if (!empty($headers['reply-to'])) {
  68. $mailgun_message['h:Reply-To'] = $headers['reply-to'];
  69. }
  70. $params = array();
  71. // Populate default settings.
  72. if (($variable = variable_get('mailgun_tracking', 'default')) !== 'default') {
  73. $params['o:tracking'] = ($variable === 'enabled');
  74. }
  75. if (($variable = variable_get('mailgun_tracking_clicks', 'default')) !== 'default') {
  76. $params['o:tracking-clicks'] = ($variable === 'enabled');
  77. }
  78. if (($variable = variable_get('mailgun_tracking_opens', 'default')) !== 'default') {
  79. $params['o:tracking-opens'] = ($variable === 'enabled');
  80. }
  81. // For a full list of allowed parameters,
  82. // see: https://documentation.mailgun.com/api-sending.html#sending.
  83. $allowed_params = array(
  84. 'o:tag',
  85. 'o:campaign',
  86. 'o:deliverytime',
  87. 'o:dkim',
  88. 'o:testmode',
  89. 'o:tracking',
  90. 'o:tracking-clicks',
  91. 'o:tracking-opens',
  92. );
  93. foreach ($message['params'] as $key => $value) {
  94. // Check if it's one of the known parameters.
  95. $allowed = in_array($key, $allowed_params) ? TRUE : FALSE;
  96. if ($allowed) {
  97. $params[$key] = $value;
  98. }
  99. // Check for custom MIME headers or custom JSON data.
  100. if (substr($key, 0, 2) == 'h:' || substr($key, 0, 2) == 'v:') {
  101. $params[$key] = $value;
  102. }
  103. // Add additional HIME headers, like Reply-to if it exists.
  104. if ($key === 'headers' && is_array($value)) {
  105. foreach ($value as $headers_key => $headers_value) {
  106. $params['h:' . $headers_key] = $headers_value;
  107. }
  108. }
  109. }
  110. // Add default tags by mail key if enabled.
  111. if (variable_get('mailgun_tagging_mailkey', TRUE)) {
  112. $params['o:tag'][] = $message['id'];
  113. }
  114. // Make sure the files provided in the attachments array exist.
  115. if (!empty($message['params']['attachments'])) {
  116. $params['attachment'] = array();
  117. foreach ($message['params']['attachments'] as $attachment) {
  118. if (is_array($attachment)) {
  119. // `filecontent` attachment key can be used by MimeMail as data
  120. // of the related file.
  121. if (array_key_exists('filecontent', $attachment)) {
  122. $temp_file = tmpfile();
  123. fwrite($temp_file, $attachment['filecontent']);
  124. $temp_f_meta_data = stream_get_meta_data($temp_file);
  125. $_attachment = array(
  126. 'filePath' => $temp_f_meta_data['uri'],
  127. );
  128. if (array_key_exists('filename', $attachment)) {
  129. $_attachment['remoteName'] = $attachment['filename'];
  130. }
  131. $params['attachment'][] = $_attachment;
  132. }
  133. elseif (array_key_exists('filepath', $attachment) && file_exists($attachment['filepath'])) {
  134. $_attachment = array(
  135. 'filePath' => $attachment['filepath'],
  136. );
  137. if (array_key_exists('filename', $attachment)) {
  138. $_attachment['remoteName'] = $attachment['filename'];
  139. }
  140. $params['attachment'][] = $_attachment;
  141. }
  142. }
  143. elseif (file_exists($attachment)) {
  144. $params['attachment'][] = $attachment;
  145. }
  146. }
  147. }
  148. $mailgun_message['params'] = $params;
  149. // Queue the message if the setting is enabled.
  150. if (variable_get('mailgun_queue', FALSE)) {
  151. $queue = DrupalQueue::get('mailgun_queue', TRUE);
  152. $queue->createItem($mailgun_message);
  153. return TRUE;
  154. }
  155. return mailgun_send($mailgun_message);
  156. }
  157. }