smtp.module
Search API
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.moduleView source
- <?php
- /**
- * @file
- * This is the main module file for SMTP, which will let us
- * send emails using SMTP instead of PHP's mail() command.
- */
-
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\Exception;
-
-
-
- // Hook_menu
- function smtp_menu() {
-
- $items = array();
-
- $items["admin/config/smtp"] = array(
- "title" => "SMTP settings",
- "description" => "Configure SMTP module",
- "page_callback" => "fp_render_form",
- "page_arguments" => array("smtp_settings_form", "system_settings"),
- "access_arguments" => array("de_can_administer_smtp"),
- "page_settings" => array(
-
- "menu_icon" => fp_get_module_path('smtp') . "/icons/email.png",
- "menu_links" => array(
- 0 => array(
- "text" => "Admin Console",
- "path" => "admin-tools/admin",
- "query" => "de_catalog_year=%DE_CATALOG_YEAR%",
- ),
- ),
- ),
- "type" => MENU_TYPE_NORMAL_ITEM,
- "tab_parent" => "admin-tools/admin",
- );
-
- return $items;
-
- }
-
-
- function smtp_settings_form() {
- $form = array();
-
- $form['mark_top'] = array(
- 'value' => '<p>Configure the connection to your SMTP server here.</p>',
- );
-
- $form['smtp_host'] = array(
- 'type' => 'textfield',
- 'label' => t('Host'),
- 'value' => variable_get('smtp_host', ''),
- 'description' => 'Ex: mail.example.com',
- );
-
- $form['smtp_port'] = array(
- 'type' => 'textfield',
- 'label' => t('Port'),
- 'value' => variable_get('smtp_port', ''),
- 'size' => 10,
- 'description' => 'Ex: 25, 587, 465',
- );
-
- $form['smtp_secure'] = array(
- 'type' => 'select',
- 'label' => t('Security'),
- 'value' => variable_get('smtp_secure', ''),
- 'options' => array('none' => 'none', 'tls' => 'TLS', 'ssl' => 'SSL'),
- 'description' => 'Ex: TLS',
- );
-
- $form['smtp_username'] = array(
- 'type' => 'textfield',
- 'label' => t('Username'),
- 'value' => variable_get('smtp_username', ''),
- 'description' => '',
- );
-
- $form['smtp_password'] = array(
- 'type' => 'textfield',
- 'label' => t('Password'),
- 'value' => variable_get('smtp_password', ''),
- 'description' => t('Note: The password will be stored within the database, in the Variables table.
- Make sure you take the necessary precautions to ensure the security of your database.'),
- );
-
-
- $form['smtp_from_email_address'] = array(
- 'type' => 'textfield',
- 'label' => t('From Email Address'),
- 'value' => variable_get('smtp_from_email_address', 'noreply@yourdomain.com'),
- 'description' => 'What is the default "from" address on emails we send?',
- );
-
- $form['smtp_from_email_name'] = array(
- 'type' => 'textfield',
- 'label' => t('From Email Name'),
- 'value' => variable_get('smtp_from_email_name', 'NoReply - FlightPath'),
- 'description' => 'What is the default "from" name on emails we send?',
- );
-
-
-
-
- return $form;
- }
-
-
- /**
- * For attachments, the array can be one of two methods:
- *
- * (1)
- * $arr["full_filename"] => "filename"
- * or
- * (2)
- * $arr['filename'] = "string that makes up atachment"
- *
- * For method #2, $bool_string_attachment must be set to TRUE.
- *
- *
- */
- function smtp_mail($to, $subject, $msg, $bool_html = FALSE, $attachments = array(), $bool_string_attachment = FALSE) {
-
- $system_path = trim($GLOBALS["fp_system_settings"]["file_system_path"]);
- require_once("$system_path/libraries/PHPMailer/src/Exception.php");
- require_once("$system_path/libraries/PHPMailer/src/PHPMailer.php");
- require_once("$system_path/libraries/PHPMailer/src/SMTP.php");
-
- $msg = trim($msg);
- if ($msg == "") {
- if (count($attachments) == 0) {
- // This is a completely empty message! Don't send anything.
- watchdog("smtp", "Not sending email to $to ($subject). No message or attachments.", array(), WATCHDOG_DEBUG);
- return FALSE;
- }
- else {
- $msg = " "; // We DO have attachments, so add some spaces so we have a string in the variable.
- }
- }
-
-
- $mail = new PHPMailer();
-
- // Settings
- $mail->IsSMTP();
- $mail->CharSet = 'UTF-8';
-
- $mail->Host = variable_get('smtp_host', '');
- $mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
- // 0 = no output, 1 = errors and messages, 2 = messages only.
-
- $mail->SMTPSecure = variable_get('smtp_secure', ''); // tls, ssl, and none are allowed.
-
- $mail->SMTPAuth = TRUE; // enable SMTP authentication
- $mail->Port = variable_get('smtp_port', ''); // set the SMTP port
- $mail->Username = variable_get('smtp_username', '');
- $mail->Password = variable_get('smtp_password', ''); // SMTP account password example
-
- // Content
- //$mail->isHTML(true); // Set email format to HTML
- //$mail->Subject = 'Here is the subject';
- //$mail->Body = 'This is the HTML message body <b>in bold!</b>';
- //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
-
- // Content
- if ($bool_html) {
- $mail->isHTML(TRUE);
- $mail->msgHTML($msg);
- }
- else {
- $mail->Body = $msg;
- }
-
- $mail->Subject = $subject;
-
-
-
- $mail->setFrom(variable_get('smtp_from_email_address', 'noreply@yourdomain.com'), variable_get('smtp_from_email_name', 'NoReply - FlightPath'));
-
-
- // Set a unique MessageID
- $temp = explode("@", variable_get('smtp_from_email_address', 'noreply@yourdomain.com'));
- $dom = $temp[1];
- $mail->MessageID = "<" . mt_rand(1,9999) . sha1(time() * mt_rand(9,999)) . sha1($msg) . "@" . $dom . ">";
-
-
- //Recipients
- $temp = explode(',', $to);
- foreach ($temp as $line) {
- $line = trim($line);
- if ($line != "") {
- $mail->addAddress($line); // Add a recipient
- }
- }
-
-
- //$mail->addAddress('ellen@example.com', 'Ellen Smith'); // Name is optional
- //$mail->addReplyTo('info@example.com', 'Information');
- //$mail->addCC('cc@example.com');
- //$mail->addBCC('bcc@example.com');
-
- if (!$bool_string_attachment) {
- if (count($attachments) > 0) {
- foreach ($attachments as $full_filename => $filename) {
- $mail->addAttachment($full_filename, $filename);
- }
- }
- }
- else {
- // This IS a string attachment
- if (count($attachments) > 0) {
- foreach ($attachments as $filename => $string) {
- $mail->addStringAttachment($string, $filename);
- }
- }
- }
-
- $x = $mail->send();
- if (!$x) {
- fpm("PHPMailer error: " . $mail->ErrorInfo);
- watchdog("smtp", "PHPMailer error: " . $mail->ErrorInfo, array(), WATCHDOG_ERROR);
- return FALSE;
- }
-
- return TRUE;
-
- } // smtp_mail
-
-
-
-
-
-
-
-
-
Functions
Name![]() |
Description |
---|---|
smtp_mail | For attachments, the array can be one of two methods: |
smtp_menu | |
smtp_settings_form |