smtp.module 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * Implements hook_help().
  18. */
  19. function smtp_help($path, $arg) {
  20. switch ($path) {
  21. case 'admin/help#smtp':
  22. return t('Allow for site emails to be sent through an SMTP server of your choice.');
  23. }
  24. }
  25. /**
  26. * Implements hook_menu().
  27. */
  28. function smtp_menu() {
  29. $items['admin/config/system/smtp'] = array(
  30. 'title' => 'SMTP Authentication Support',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('smtp_admin_settings'),
  33. 'access arguments' => array('administer smtp module'),
  34. 'description' => 'Allow for site emails to be sent through an SMTP server of your choice.',
  35. 'file' => 'smtp.admin.inc',
  36. );
  37. return $items;
  38. }
  39. /**
  40. * Implements hook_permission().
  41. */
  42. function smtp_permission() {
  43. return array(
  44. 'administer smtp module' => array(
  45. 'title' => t('Administer SMTP Authentication Support module'),
  46. 'description' => t('Perform administration tasks for SMTP Authentication Support module.'))
  47. );
  48. }
  49. /**
  50. * Implements hook_mail().
  51. */
  52. function smtp_mail($key, &$message, $params) {
  53. if ($key == 'smtp-test') {
  54. $message['subject'] = $params['subject'];
  55. $message['body'] = $params['body'];
  56. }
  57. }
  58. /**
  59. * Implementation of hook_cron_queue_info().
  60. */
  61. function smtp_cron_queue_info() {
  62. $queues['smtp_send_queue'] = array(
  63. 'worker callback' => 'smtp_send_queue_runner',
  64. 'time' => 60, // This is the max run time per cron run in seconds.
  65. );
  66. return $queues;
  67. }
  68. /**
  69. * smtp_send_queue queuer.
  70. */
  71. function smtp_send_queue($mailerObj) {
  72. $queue = DrupalQueue::get('smtp_send_queue');
  73. $queue->createItem($mailerObj);
  74. }
  75. function smtp_send_queue_runner($variables) {
  76. _smtp_mailer_send($variables);
  77. }
  78. function _smtp_mailer_send($variables) {
  79. $mailer = $variables['mailer'];
  80. $to = $variables['to'];
  81. $from = $variables['from'];
  82. // Let the people know what is going on.
  83. watchdog('smtp', 'Sending mail to: @to', array('@to' => $to));
  84. // Try to send e-mail. If it fails, set watchdog entry.
  85. if (!$mailer->Send()) {
  86. watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mailer->ErrorInfo), WATCHDOG_ERROR);
  87. return FALSE;
  88. }
  89. $mailer->SmtpClose();
  90. return TRUE;
  91. }