mailgun.mail.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // If a text format is specified in Mailgun settings, run the message through it.
  25. $format = variable_get('mailgun_format', '_none');
  26. if ($format != '_none') {
  27. $message['body'] = check_markup($message['body'], $format);
  28. }
  29. return $message;
  30. }
  31. /**
  32. * Send the e-mail message.
  33. *
  34. * @see drupal_mail()
  35. * @see https://documentation.mailgun.com/api-sending.html#sending
  36. *
  37. * @param array $message
  38. * A message array, as described in hook_mail_alter(). $message['params'] may contain additional parameters. See mailgun_send().
  39. *
  40. * @return bool
  41. * TRUE if the mail was successfully accepted or queued, FALSE otherwise.
  42. */
  43. public function mail(array $message) {
  44. // Build the Mailgun message array.
  45. $mailgun_message = array(
  46. 'from' => $message['from'],
  47. 'to' => $message['to'],
  48. 'subject' => $message['subject'],
  49. 'text' => check_plain($message['body']),
  50. 'html' => $message['body'],
  51. );
  52. // Add CC, BCC and Reply-To fields if not empty.
  53. $headers = array_change_key_case($message['headers']);
  54. if (!empty($headers['cc'])) {
  55. $mailgun_message['cc'] = $headers['cc'];
  56. }
  57. if (!empty($headers['bcc'])) {
  58. $mailgun_message['bcc'] = $headers['bcc'];
  59. }
  60. if (!empty($headers['reply-to'])) {
  61. $mailgun_message['h:Reply-To'] = $headers['reply-to'];
  62. }
  63. $params = array();
  64. // Populate default settings.
  65. if ($variable = variable_get('mailgun_tracking', 'default') != 'default') {
  66. $params['o:tracking'] = $variable;
  67. }
  68. if ($variable = variable_get('mailgun_tracking_clicks', 'default') != 'default') {
  69. $params['o:tracking-clicks'] = $variable;
  70. }
  71. if ($variable = variable_get('mailgun_tracking_opens', 'default') != 'default') {
  72. $params['o:tracking-opens'] = $variable;
  73. }
  74. // For a full list of allowed parameters, see: https://documentation.mailgun.com/api-sending.html#sending.
  75. $allowed_params = array('o:tag', 'o:campaign', 'o:deliverytime', 'o:dkim', 'o:testmode', 'o:tracking', 'o:tracking-clicks', 'o:tracking-opens');
  76. foreach ($message['params'] as $key => $value) {
  77. // Check if it's one of the known parameters.
  78. $allowed = (in_array($key, $allowed_params)) ? TRUE : FALSE;
  79. // If more options become available but are not yet supported by the module, uncomment the following line.
  80. //$allowed = (substr($key, 0, 2) == 'o:') ? TRUE : FALSE;
  81. if ($allowed) {
  82. $params[$key] = $value;
  83. }
  84. // Check for custom MIME headers or custom JSON data.
  85. if (substr($key, 0, 2) == 'h:' || substr($key, 0, 2) == 'v:') {
  86. $params[$key] = $value;
  87. }
  88. }
  89. // Make sure the files provided in the attachments array exist.
  90. if (!empty($message['params']['attachments'])) {
  91. $params['attachments'] = array();
  92. foreach ($message['params']['attachments'] as $attachment) {
  93. if (file_exists($attachment)) {
  94. $params['attachments'][] = $attachment;
  95. }
  96. }
  97. }
  98. $mailgun_message['params'] = $params;
  99. // Queue the message if the setting is enabled.
  100. if (variable_get('mailgun_queue', FALSE)) {
  101. $queue = DrupalQueue::get('mailgun_queue', TRUE);
  102. $queue->createItem($mailgun_message);
  103. return TRUE;
  104. }
  105. return mailgun_send($mailgun_message);
  106. }
  107. }