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. // Hook_menu
  10. function smtp_menu() {
  11. $items = array();
  12. $items["admin/config/smtp"] = array(
  13. "title" => "SMTP settings",
  14. "description" => "Configure SMTP module",
  15. "page_callback" => "fp_render_form",
  16. "page_arguments" => array("smtp_settings_form", "system_settings"),
  17. "access_arguments" => array("de_can_administer_smtp"),
  18. "page_settings" => array(
  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 Name'),
  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. /**
  86. * For attachments, the array can be one of two methods:
  87. *
  88. * (1)
  89. * $arr["full_filename"] => "filename"
  90. * or
  91. * (2)
  92. * $arr['filename'] = "string that makes up atachment"
  93. *
  94. * For method #2, $bool_string_attachment must be set to TRUE.
  95. *
  96. *
  97. */
  98. function smtp_mail($to, $subject, $msg, $bool_html = FALSE, $attachments = array(), $bool_string_attachment = FALSE) {
  99. $system_path = trim($GLOBALS["fp_system_settings"]["file_system_path"]);
  100. require_once("$system_path/libraries/PHPMailer/src/Exception.php");
  101. require_once("$system_path/libraries/PHPMailer/src/PHPMailer.php");
  102. require_once("$system_path/libraries/PHPMailer/src/SMTP.php");
  103. $msg = trim($msg);
  104. if ($msg == "") {
  105. if (count($attachments) == 0) {
  106. // This is a completely empty message! Don't send anything.
  107. watchdog("smtp", "Not sending email to $to ($subject). No message or attachments.", array(), WATCHDOG_DEBUG);
  108. return FALSE;
  109. }
  110. else {
  111. $msg = " "; // We DO have attachments, so add some spaces so we have a string in the variable.
  112. }
  113. }
  114. $mail = new PHPMailer();
  115. // Settings
  116. $mail->IsSMTP();
  117. $mail->CharSet = 'UTF-8';
  118. $mail->Host = variable_get('smtp_host', '');
  119. $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
  120. // 0 = no output, 1 = errors and messages, 2 = messages only.
  121. $mail->SMTPSecure = variable_get('smtp_secure', ''); // tls, ssl, and none are allowed.
  122. $mail->SMTPAuth = TRUE; // enable SMTP authentication
  123. $mail->Port = variable_get('smtp_port', ''); // set the SMTP port
  124. $mail->Username = variable_get('smtp_username', '');
  125. $mail->Password = variable_get('smtp_password', ''); // SMTP account password example
  126. // Content
  127. //$mail->isHTML(true); // Set email format to HTML
  128. //$mail->Subject = 'Here is the subject';
  129. //$mail->Body = 'This is the HTML message body <b>in bold!</b>';
  130. //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  131. // Content
  132. if ($bool_html) {
  133. $mail->isHTML(TRUE);
  134. $mail->msgHTML($msg);
  135. }
  136. else {
  137. $mail->Body = $msg;
  138. }
  139. $mail->Subject = $subject;
  140. $mail->setFrom(variable_get('smtp_from_email_address', 'noreply@yourdomain.com'), variable_get('smtp_from_email_name', 'NoReply - FlightPath'));
  141. //Recipients
  142. $temp = explode(',', $to);
  143. foreach ($temp as $line) {
  144. $line = trim($line);
  145. if ($line != "") {
  146. $mail->addAddress($line); // Add a recipient
  147. }
  148. }
  149. //$mail->addAddress('ellen@example.com', 'Ellen Smith'); // Name is optional
  150. //$mail->addReplyTo('info@example.com', 'Information');
  151. //$mail->addCC('cc@example.com');
  152. //$mail->addBCC('bcc@example.com');
  153. if (!$bool_string_attachment) {
  154. if (count($attachments) > 0) {
  155. foreach ($attachments as $full_filename => $filename) {
  156. $mail->addAttachment($full_filename, $filename);
  157. }
  158. }
  159. }
  160. else {
  161. // This IS a string attachment
  162. if (count($attachments) > 0) {
  163. foreach ($attachments as $filename => $string) {
  164. $mail->addStringAttachment($string, $filename);
  165. }
  166. }
  167. }
  168. $x = $mail->send();
  169. if (!$x) {
  170. fpm("PHPMailer error: " . $mail->ErrorInfo);
  171. watchdog("smtp", "PHPMailer error: " . $mail->ErrorInfo, array(), WATCHDOG_ERROR);
  172. return FALSE;
  173. }
  174. return TRUE;
  175. } // smtp_mail

Functions

Namesort descending Description
smtp_mail For attachments, the array can be one of two methods:
smtp_menu
smtp_settings_form