smtp.module

  1. 6.x modules/smtp/smtp.module
  2. 5.x modules/smtp/smtp.module

This is the main module file for SMTP, which will let us send emails using SMTP instead of PHP's mail() command.

File

modules/smtp/smtp.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is the main module file for SMTP, which will let us
  5. * send emails using SMTP instead of PHP's mail() command.
  6. */
  7. use PHPMailer\PHPMailer\PHPMailer;
  8. use PHPMailer\PHPMailer\Exception;
  9. function smtp_menu() {
  10. $items = array();
  11. $items["admin/config/smtp"] = array(
  12. "title" => "SMTP settings",
  13. "description" => "Configure SMTP module",
  14. "page_callback" => "fp_render_form",
  15. "page_arguments" => array("smtp_settings_form", "system_settings"),
  16. "access_arguments" => array("de_can_administer_smtp"),
  17. "page_settings" => array(
  18. "page_has_search" => FALSE,
  19. "menu_icon" => fp_get_module_path('smtp') . "/icons/email.png",
  20. "menu_links" => array(
  21. 0 => array(
  22. "text" => "Admin Console",
  23. "path" => "admin-tools/admin",
  24. "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
  25. ),
  26. ),
  27. ),
  28. "type" => MENU_TYPE_NORMAL_ITEM,
  29. "tab_parent" => "admin-tools/admin",
  30. );
  31. return $items;
  32. }
  33. function smtp_settings_form() {
  34. $form = array();
  35. $form['mark_top'] = array(
  36. 'value' => '<p>Configure the connection to your SMTP server here.</p>',
  37. );
  38. $form['smtp_host'] = array(
  39. 'type' => 'textfield',
  40. 'label' => t('Host'),
  41. 'value' => variable_get('smtp_host', ''),
  42. 'description' => 'Ex: mail.example.com',
  43. );
  44. $form['smtp_port'] = array(
  45. 'type' => 'textfield',
  46. 'label' => t('Port'),
  47. 'value' => variable_get('smtp_port', ''),
  48. 'size' => 10,
  49. 'description' => 'Ex: 25, 587, 465',
  50. );
  51. $form['smtp_secure'] = array(
  52. 'type' => 'select',
  53. 'label' => t('Security'),
  54. 'value' => variable_get('smtp_secure', ''),
  55. 'options' => array('none' => 'none', 'tls' => 'TLS', 'ssl' => 'SSL'),
  56. 'description' => 'Ex: TLS',
  57. );
  58. $form['smtp_username'] = array(
  59. 'type' => 'textfield',
  60. 'label' => t('Username'),
  61. 'value' => variable_get('smtp_username', ''),
  62. 'description' => '',
  63. );
  64. $form['smtp_password'] = array(
  65. 'type' => 'textfield',
  66. 'label' => t('Password'),
  67. 'value' => variable_get('smtp_password', ''),
  68. 'description' => t('Note: The password will be stored within the database, in the Variables table.
  69. Make sure you take the necessary precautions to ensure the security of your database.'),
  70. );
  71. $form['smtp_from_email_address'] = array(
  72. 'type' => 'textfield',
  73. 'label' => t('From Email Address'),
  74. 'value' => variable_get('smtp_from_email_address', 'noreply@yourdomain.com'),
  75. 'description' => 'What is the default "from" address on emails we send?',
  76. );
  77. $form['smtp_from_email_name'] = array(
  78. 'type' => 'textfield',
  79. 'label' => t('From Email Address'),
  80. 'value' => variable_get('smtp_from_email_name', 'NoReply - FlightPath'),
  81. 'description' => 'What is the default "from" name on emails we send?',
  82. );
  83. return $form;
  84. }
  85. function smtp_mail($to, $subject, $msg) {
  86. $system_path = trim($GLOBALS["fp_system_settings"]["file_system_path"]);
  87. require_once("$system_path/inc/PHPMailer/src/Exception.php");
  88. require_once("$system_path/inc/PHPMailer/src/PHPMailer.php");
  89. require_once("$system_path/inc/PHPMailer/src/SMTP.php");
  90. $mail = new PHPMailer();
  91. // Settings
  92. $mail->IsSMTP();
  93. $mail->CharSet = 'UTF-8';
  94. $mail->Host = variable_get('smtp_host', '');
  95. $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
  96. // 0 = no output, 1 = errors and messages, 2 = messages only.
  97. $mail->SMTPSecure = variable_get('smtp_secure', ''); // tls, ssl, and none are allowed.
  98. $mail->SMTPAuth = true; // enable SMTP authentication
  99. $mail->Port = variable_get('smtp_port', ''); // set the SMTP port
  100. $mail->Username = variable_get('smtp_username', '');
  101. $mail->Password = variable_get('smtp_password', ''); // SMTP account password example
  102. // Content
  103. //$mail->isHTML(true); // Set email format to HTML
  104. //$mail->Subject = 'Here is the subject';
  105. //$mail->Body = 'This is the HTML message body <b>in bold!</b>';
  106. //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  107. // Content
  108. $mail->Subject = $subject;
  109. $mail->Body = $msg;
  110. $mail->setFrom(variable_get('smtp_from_email_address', 'noreply@yourdomain.com'), variable_get('smtp_from_email_name', 'NoReply - FlightPath'));
  111. //Recipients
  112. $temp = explode(',', $to);
  113. foreach ($temp as $line) {
  114. $line = trim($line);
  115. if ($line != "") {
  116. $mail->addAddress($line); // Add a recipient
  117. }
  118. }
  119. //$mail->addAddress('ellen@example.com', 'Ellen Smith'); // Name is optional
  120. //$mail->addReplyTo('info@example.com', 'Information');
  121. //$mail->addCC('cc@example.com');
  122. //$mail->addBCC('bcc@example.com');
  123. $x = $mail->send();
  124. if (!$x) {
  125. fpm("PHPMailer error: " . $mail->ErrorInfo);
  126. // TODO: Log error?
  127. }
  128. }

Functions