smtp.module 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Implementation of 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. * smtp_send_queue queuer.
  86. */
  87. function smtp_send_queue($mailerObj) {
  88. $queue = DrupalQueue::get('smtp_send_queue');
  89. $queue->createItem($mailerObj);
  90. }
  91. function smtp_send_queue_runner($message) {
  92. $logging = variable_get('smtp_debugging', SMTP_LOGGING_ERRORS);
  93. // Legacy for mails queued before 7.x-v1.3
  94. // What was passed to the runner used to be a PHPMailer object, not a message array.
  95. if (!empty($message['mailer']) && is_object($message['mailer'])) {
  96. if (!$message['mailer']->Send()) {
  97. if ($logging == SMTP_LOGGING_ALL || $logging == SMTP_LOGGING_ERRORS) {
  98. watchdog('smtp', 'Error sending e-mail from @from to @to, will retry on cron run : !error_message.',
  99. array(
  100. '@from' => $message['from'],
  101. '@to' => $message['to'],
  102. '!error_message' => $message['mailer']->ErrorInfo,
  103. ), WATCHDOG_ERROR);
  104. }
  105. }
  106. return;
  107. }
  108. // Let the people know what is going on.
  109. if ($logging == SMTP_LOGGING_ALL) {
  110. watchdog('smtp', 'Sending mail to: @to', array('@to' => $message['to']));
  111. }
  112. // Send the message.
  113. $mail_system = new SmtpMailSystem();
  114. $mail_system->mailWithoutQueue($message);
  115. }
  116. /**
  117. * Store failed messages for later.
  118. *
  119. * @param array $new_message
  120. *
  121. * @return array
  122. * All messages that have been saved.
  123. */
  124. function smtp_failed_messages($message) {
  125. $queue = DrupalQueue::get('smtp_failure_queue');
  126. $queue->createItem($message);
  127. }
  128. /**
  129. * Queue runner for smtp_failure_queue.
  130. *
  131. * @param $message
  132. * A drupal_mail-formatted message.
  133. */
  134. function smtp_failure_queue_runner($message) {
  135. $queue = DrupalQueue::get('smtp_send_queue');
  136. $queue->createItem($message);
  137. }