'SMTP Authentication Support', 'page callback' => 'drupal_get_form', 'page arguments' => array('smtp_admin_settings'), 'access arguments' => array('administer site configuration'), 'description' => 'Allows the sending of site e-mail through an SMTP server of your choice.', ); return $items; } // End of smtp_menu(). /** * Administrative settings. * * @return * An array containing form items to place on the module settings page. */ function smtp_admin_settings() { // Override the smtp_library variable. if (variable_get('smtp_on', 0)) { $smtp_path = drupal_get_filename('module', 'smtp'); if ($smtp_path) { variable_set('smtp_library', $smtp_path); drupal_set_message(t('SMTP.module is active.')); } // If drupal can't find the path to the module, display an error. else { drupal_set_message(t("SMTP.module error: Can't find file."), 'error'); } } // If this module is turned off, delete the variable. else { variable_del('smtp_library'); drupal_set_message(t('SMTP.module is INACTIVE.')); } $form['onoff'] = array( '#type' => 'fieldset', '#title' => t('Install options'), ); $form['onoff']['smtp_on'] = array( '#type' => 'radios', '#title' => t('Turn this module on or off'), '#default_value' => variable_get('smtp_on', 0), '#options' => array(1 => t('On'), 0 => t('Off')), '#description' => t('To uninstall this module you must turn it off here first.'), ); $form['server'] = array( '#type' => 'fieldset', '#title' => t('SMTP server settings'), ); $form['server']['smtp_host'] = array( '#type' => 'textfield', '#title' => t('SMTP server'), '#default_value' => variable_get('smtp_host', ''), '#description' => t('The address of your outgoing SMTP server.'), ); $form['server']['smtp_hostbackup'] = array( '#type' => 'textfield', '#title' => t('SMTP backup server'), '#default_value' => variable_get('smtp_hostbackup', ''), '#description' => t('The address of your outgoing SMTP backup server. If the primary server can\'t be found this one will be tried. This is optional.'), ); $form['server']['smtp_port'] = array( '#type' => 'textfield', '#title' => t('SMTP port'), '#size' => 6, '#maxlength' => 6, '#default_value' => variable_get('smtp_port', '25'), '#description' => t('The default SMTP port is 25, if that is being blocked try 80. Gmail uses 465. See !url for more information on configuring for use with Gmail.', array('!url' => l(t('this page'), 'http://gmail.google.com/support/bin/answer.py?answer=13287'))), ); // Only display the option if openssl is installed. if (function_exists('openssl_open')) { $encryption_options = array( 'standard' => t('No'), 'ssl' => t('Use SSL'), 'tls' => t('Use TLS'), ); $encryption_description = t('This allows connection to an SMTP server that requires SSL encryption such as Gmail.'); } // If openssl is not installed, use normal protocol. else { variable_set('smtp_protocol', 'standard'); $encryption_options = array('standard' => t('No')); $encryption_description = t('Your PHP installation does not have SSL enabled. See the !url page on php.net for more information. Gmail requires SSL.', array('!url' => l(t('OpenSSL Functions'), 'http://php.net/openssl'))); } $form['server']['smtp_protocol'] = array( '#type' => 'select', '#title' => t('Use encrypted protocol'), '#default_value' => variable_get('smtp_protocol', 'standard'), '#options' => $encryption_options, '#description' => $encryption_description, ); $form['auth'] = array( '#type' => 'fieldset', '#title' => t('SMTP Authentication'), '#description' => t('Leave blank if your SMTP server does not require authentication.'), ); $form['auth']['smtp_username'] = array( '#type' => 'textfield', '#title' => t('Username'), '#default_value' => variable_get('smtp_username', ''), '#description' => t('SMTP Username.'), ); $form['auth']['smtp_password'] = array( '#type' => 'textfield', '#title' => t('Password'), '#default_value' => variable_get('smtp_password', ''), '#description' => t('SMTP password.'), ); $form['email_options'] = array( '#type' => 'fieldset', '#title' => t('E-mail options'), ); $form['email_options']['smtp_from'] = array( '#type' => 'textfield', '#title' => t('E-mail from address'), '#default_value' => variable_get('smtp_from', ''), '#description' => t('The e-mail address that all e-mails will be from.'), ); $form['email_options']['smtp_fromname'] = array( '#type' => 'textfield', '#title' => t('E-mail from name'), '#default_value' => variable_get('smtp_fromname', ''), '#description' => t('The name that all e-mails will be from. If left blank will use the site name of: ') . variable_get('site_name', 'Drupal powered site'), ); // If an address was given, send a test e-mail message. $test_address = variable_get('smtp_test_address', ''); if ($test_address != '') { // Clear the variable so only one message is sent. variable_del('smtp_test_address'); global $language; $params['subject'] = t('Drupal test e-mail'); $params['body'] = t('If you receive this message it means your site is capable of sending e-mail.'); drupal_mail('smtp', 'smtp-test', $test_address, $language, $params); drupal_set_message(t('A test e-mail has been sent to @email. You may want to !check for any error messages.', array('@email' => $test_address, '!check' => l(t('check the logs'), 'admin/reports/watchdog')))); } $form['email_test'] = array( '#type' => 'fieldset', '#title' => t('Send test e-mail'), ); $form['email_test']['smtp_test_address'] = array( '#type' => 'textfield', '#title' => t('E-mail address to send a test e-mail to'), '#default_value' => '', '#description' => t('Type in an address to have a test e-mail sent there.'), ); $form['smtp_debugging'] = array( '#type' => 'checkbox', '#title' => t('Enable debugging'), '#default_value' => variable_get('smtp_debugging', 0), '#description' => t('Checking this box will print SMTP messages from the server for every e-mail that is sent.'), ); return system_settings_form($form); } // End of smtp_admin_settings(). /** * Validataion for the administrative settings form. * * @param form * An associative array containing the structure of the form. * @param form_state * A keyed array containing the current state of the form. */ function smtp_admin_settings_validate($form, &$form_state) { if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_host'] == '') { form_set_error('smtp_host', t('You must enter an SMTP server address.')); } if ($form_state['values']['smtp_on'] == 1 && $form_state['values']['smtp_port'] == '') { form_set_error('smtp_port', t('You must enter an SMTP port number.')); } if ($form_state['values']['smtp_from'] && !valid_email_address($form_state['values']['smtp_from'])) { form_set_error('smtp_from', t('The provided from e-mail address is not valid.')); } } // End of smtp_admin_settings_validate(). /** * Sends out the e-mail. * * @param message * An array with at least the following elements: id, to, subject, body and * headers. * * @see http://api.drupal.org/api/function/drupal_mail_send/6 */ function drupal_mail_wrapper($message) { $id = $message['id']; $to = $message['to']; $from = $message['from']; $header = $message['headers']; $subject = $message['subject']; $body = $message['body']; // Include the PHPMailer class (which includes the SMTP class). require_once(drupal_get_path('module', 'smtp') .'/phpmailer/class.phpmailer.php'); // Create a new PHPMailer object. $mail = new PHPMailer(); global $language; if ($language) { $mail->SetLanguage($language->language, drupal_get_path('module', 'smtp') .'/phpmailer/language/'); } if (variable_get('smtp_debugging', 0) == 1) { $mail->SMTPDebug = TRUE; } $username = variable_get('smtp_username', ''); $password = variable_get('smtp_password', ''); // Set the default name e-mails should be from. // If value is not defined in settings, use site_name. if (variable_get('smtp_fromname', '') != '') { $from_name = variable_get('smtp_fromname', ''); } else { // Blank value will let the e-mail address appear. $from_name = variable_get('site_name', ''); } // If from e-mail address is blank, use smtp_from config option. if ($from == NULL || $from == '') { if (variable_get('smtp_from', '') != '') { $from = variable_get('smtp_from', ''); } else { // If smtp_from config option is blank, use site_email. $from = variable_get('site_email', ''); } } if (preg_match('/^".*"\s*<.*>$/', $from)) { $from_name = preg_replace('/"(.*)"(.*)/i', '$1', $from); // It gives: Name $from = preg_replace("/(.*)\<(.*)\>/i", '$2', $from); // It gives: name@domain.tld } else if (!valid_email_address($from)) { $from_error_message = t('The submitted from address (@from) is not valid.', array('@from' => $from)); drupal_set_message($from_error_message, 'error'); watchdog('smtp', $from_error_message, WATCHDOG_ERROR); return false; } // Defines the From value to what we expect. // This should be done correctly by PHPMailer now. // $mail->From = '"'. $from_name .'" <'. $from .'>'; $mail->From = $from; $mail->FromName = $from_name; $mail->Sender = $from; // Decide whether to use SMTP Authorization. if ($username != '' and $password != '') { // Do so if username and password are given. $auth = TRUE; } else { $auth = FALSE; } // Force the initial value to be set to text/plain. $mail->IsHTML(FALSE); // Take care of the e-mail headers. foreach ($header as $key => $value) { //watchdog('error', 'Key: ' . $key . ' Value: ' . $value); if (drupal_strtolower($key) == 'from') { if ($from == NULL or $from == '') { // If a from value was already given, then set based on header. // Should be the most common situation since drupal_mail moves the // from to headers. $from = $value; $mail->From = $value; // then from can be out of sync with from_name ! $mail->FromName = ''; $mail->Sender = $value; } } else if (drupal_strtolower($key) == 'content-type' && strpos(drupal_strtolower($value), 'text/html') !== FALSE) { $mail->IsHTML(TRUE); } else if (drupal_strtolower($key) == 'content-type' && strpos(drupal_strtolower($value), 'multipart/mixed') !== FALSE) { // $body passed to smtp should already be formatted. Add multipart // header and tell phpmailer to leave it alone $mail->AddCustomHeader($key .': '. $value); $mail->message_type = "pre"; $mail->ContentType = 'multipart/mixed'; } else if (drupal_strtolower($key) == 'reply-to') { // Only add a "reply-to" if it's not the same as "return-path". if ($value != $header['Return-Path']) { $mail->AddReplyTo($value); } } else if (drupal_strtolower($key) == 'return-path') { if (trim($value) != '') { // This is be set by SmtpSend() // $mail->Sender = $value; } } else if (drupal_strtolower($key) == 'content-transfer-encoding') { $mail->Encoding = $value; } else if (drupal_strtolower($key) == 'mime-version') { // Ommit MIME-Version, since it will be set by PHPMailer. } else if (drupal_strtolower($key) == 'x-mailer') { // Ommit X-Mailer, since it will be set by PHPMailer. } else if (drupal_strtolower($key) == 'errors-to') { // Prefer to keep control on this one, it will be set by PHPMailer. } else if (drupal_strtolower($key) == 'bcc') { $bccrecipients = split(",", $value); foreach ($bccrecipients as $bccrecipient) { if (strpos($bccrecipient, '<') !== false) { $bccparts = explode(" <", $bccrecipient); $bccname = $bccparts[0]; $bccaddr = rtrim($bccparts[1], ">"); } else { $bccname = ""; $bccaddr = $bccrecipient; } $mail->AddBCC($bccaddr, $bccname); } } // Else the header key is not special. else { // Add header line. $mail->AddCustomHeader($key .': '. $value); } } // Set the correct protocol prefix to append to the smtp host. switch (variable_get('smtp_protocol', 'standard')) { case "ssl": $mail->SMTPSecure = 'ssl'; break; case "tls": $mail->SMTPSecure = 'tls'; break; case "standard": $mail->SMTPSecure = ''; } $mail->Host = variable_get('smtp_host', '') .';'. variable_get('smtp_hostbackup', ''); $mail->Port = variable_get('smtp_port', '25'); $mail->Mailer = 'smtp'; $mail->SMTPAuth = $auth; $mail->Username = $username; $mail->Password = $password; $mail->CharSet = 'utf-8'; $mail->AddCustomHeader('Errors-To: '. $from); $torecipients = split(',', $to); foreach ($torecipients as $torecipient) { if (strpos($torecipient, '<') !== false) { $toparts = explode(' <', $torecipient); $toname = $toparts[0]; $toaddr = rtrim($toparts[1], '>'); } else { $toname = ''; $toaddr = $torecipient; } $mail->AddAddress($toaddr, $toname); } $mail->Subject = $subject; $mail->Body = $body; watchdog('smtp', 'Sending mail to: @to', array('@to' => $to)); // Try to send e-mail. If it fails, set watchdog entry. if (!$mail->Send()) { watchdog('smtp', 'Error sending e-mail from @from to @to : !error_message', array('@from' => $from, '@to' => $to, '!error_message' => $mail->ErrorInfo), WATCHDOG_ERROR); return false; } $mail->SmtpClose(); return true; } // End of drupal_mail_wrapper(). /** * Implementation of hook_mail(). */ function smtp_mail($key, &$message, $params) { if ($key == 'smtp-test') { $message['subject'] = $params['subject']; $message['body'] = $params['body']; } } // End of smtp_mail().