smtp.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @file
  4. * Enables Drupal to send e-mail directly to an SMTP server.
  5. *
  6. * This module uses a customized extract of the PHPMailer
  7. * library (originally by Brent R. Matzelle, now maintained
  8. * by Codeworx Tech.) relicensed from LGPL to GPL, included
  9. * as a part of the module.
  10. *
  11. * Overriding mail handling in Drupal to make SMTP the default
  12. * transport layer, requires to change the mail_system variable's
  13. * default value array('default-system' => 'DefaultMailSystem').
  14. * This module uses array('default-system' => 'SmtpMailSystem').
  15. */
  16. /**
  17. * SMTP logging -- logging is disabled
  18. */
  19. define('SMTP_LOGGING_NONE', 0);
  20. /**
  21. * SMTP logging -- all messages are logged
  22. */
  23. define('SMTP_LOGGING_ALL', 1);
  24. /**
  25. * SMTP logging -- only errors are logged
  26. */
  27. define('SMTP_LOGGING_ERRORS', 2);
  28. /**
  29. * Implements hook_help().
  30. */
  31. function smtp_help($path, $arg) {
  32. switch ($path) {
  33. case 'admin/help#smtp':
  34. return t('Allow for site emails to be sent through an SMTP server of your choice.');
  35. }
  36. }
  37. /**
  38. * Implements hook_menu().
  39. */
  40. function smtp_menu() {
  41. $items['admin/config/system/smtp'] = array(
  42. 'title' => 'SMTP Authentication Support',
  43. 'page callback' => 'drupal_get_form',
  44. 'page arguments' => array('smtp_admin_settings'),
  45. 'access arguments' => array('administer smtp module'),
  46. 'description' => 'Allow for site emails to be sent through an SMTP server of your choice.',
  47. 'file' => 'smtp.admin.inc',
  48. );
  49. return $items;
  50. }
  51. /**
  52. * Implements hook_permission().
  53. */
  54. function smtp_permission() {
  55. return array(
  56. 'administer smtp module' => array(
  57. 'title' => t('Administer SMTP Authentication Support module'),
  58. 'description' => t('Perform administration tasks for SMTP Authentication Support module.'))
  59. );
  60. }
  61. /**
  62. * Implements hook_mail().
  63. */
  64. function smtp_mail($key, &$message, $params) {
  65. if ($key == 'smtp-test') {
  66. $message['subject'] = $params['subject'];
  67. $message['body'] = $params['body'];
  68. }
  69. }
  70. /**
  71. * Implements hook_cron_queue_info().
  72. */
  73. function smtp_cron_queue_info() {
  74. $queues['smtp_send_queue'] = array(
  75. 'worker callback' => 'smtp_send_queue_runner',
  76. 'time' => 60,
  77. );
  78. $queues['smtp_failure_queue'] = array(
  79. 'worker callback' => 'smtp_failure_queue_runner',
  80. 'time' => 30,
  81. );
  82. return $queues;
  83. }
  84. /**
  85. * Function to queue a message into the Drupal system queue.
  86. *
  87. * @param object
  88. */
  89. function smtp_send_queue($mailer_object) {
  90. $queue = DrupalQueue::get('smtp_send_queue');
  91. $queue->createItem($mailer_object);
  92. }
  93. /**
  94. * Queue runner function defined in hook_cron_queue_info() above.
  95. *
  96. * @param $message
  97. *
  98. * @return bool
  99. */
  100. function smtp_send_queue_runner($message) {
  101. $logging = variable_get('smtp_debugging', SMTP_LOGGING_ERRORS);
  102. // Legacy for mails queued before 7.x-v1.3
  103. // What was passed to the runner used to be a PHPMailer object, not a message array.
  104. if (!empty($message['mailer']) && is_object($message['mailer'])) {
  105. if (!$message['mailer']->Send()) {
  106. if ($logging == SMTP_LOGGING_ALL || $logging == SMTP_LOGGING_ERRORS) {
  107. watchdog('smtp', 'Error sending e-mail from @from to @to, will retry on cron run : !error_message.',
  108. array(
  109. '@from' => $message['from'],
  110. '@to' => $message['to'],
  111. '!error_message' => $message['mailer']->ErrorInfo,
  112. ), WATCHDOG_ERROR);
  113. }
  114. }
  115. return;
  116. }
  117. // Let the people know what is going on.
  118. if ($logging == SMTP_LOGGING_ALL) {
  119. watchdog('smtp', 'Sending mail to: @to', array('@to' => $message['to']));
  120. }
  121. // Send the message.
  122. $mail_system = new SmtpMailSystem();
  123. $mail_system->mailWithoutQueue($message);
  124. }
  125. /**
  126. * Store failed messages for later.
  127. *
  128. * @param array $new_message
  129. *
  130. * @return array
  131. * All messages that have been saved.
  132. */
  133. function smtp_failed_messages($message) {
  134. $queue = DrupalQueue::get('smtp_failure_queue');
  135. $queue->createItem($message);
  136. }
  137. /**
  138. * Queue runner for smtp_failure_queue.
  139. *
  140. * @param $message
  141. * A drupal_mail-formatted message.
  142. */
  143. function smtp_failure_queue_runner($message) {
  144. $queue = DrupalQueue::get('smtp_send_queue');
  145. $queue->createItem($message);
  146. }
  147. /**
  148. * Implements hook_mailengine().
  149. */
  150. function smtp_mailengine($op, $message = array()) {
  151. switch ($op) {
  152. case 'list':
  153. return array(
  154. 'name' => t('SMTP'),
  155. 'description' => t('Mailing engine using the SMTP library.'),
  156. );
  157. case 'settings':
  158. $form['info']['#markup'] = t('To configure your mail server settings, visit the <a href="@url">SMTP settings page</a>.', array('@url' => url('admin/config/system/smtp')));
  159. return $form;
  160. case 'multiple':
  161. case 'single':
  162. case 'send':
  163. if (isset($message['address']) && !is_array($message['address'])) {
  164. $message['address'] = array($message['address']);
  165. }
  166. $message['headers']['Content-Type'] = str_replace('multipart/alternative', 'multipart/mixed', $message['headers']['Content-Type']);
  167. // Send the message.
  168. $mail_system = new SmtpMailSystem();
  169. return $mail_system->mail($message);
  170. }
  171. return FALSE;
  172. }