You are here

messaging_phpmailer.module in Messaging 6.4

HTML Mail using PHPMailer. Messaging method plug-in.

File

messaging_phpmailer/messaging_phpmailer.module
View source
<?php

/**
 * @file
 * HTML Mail using PHPMailer. Messaging method plug-in.
 */

/**
 * Implementation of hook_menu().
 */
function messaging_phpmailer_menu() {
  $items = array();
  $items['admin/messaging/settings/method/phpmailer'] = array(
    'title' => 'PHPMailer',
    'description' => 'Configure PHPMailer.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'messaging_phpmailer_settings_form',
    ),
    'access arguments' => array(
      'administer messaging',
    ),
    'type' => MENU_LOCAL_TASK,
    'filter' => 'messaging_html',
    // Default filter for this format
    'anonymous' => TRUE,
  );
  return $items;
}

/**
 * Implementation of hook_messaging
 */
function messaging_phpmailer_messaging($op = 'info') {
  switch ($op) {
    case 'send methods':
      $info['phpmailer'] = array(
        'title' => 'PHPMailer',
        'name' => t('HTML Mail'),
        'group' => 'mail',
        'send callback' => 'messaging_phpmailer_send_msg',
        'glue' => "<br>",
        // don't use <br/> nor <br /> for maximum HTML email client compatibility
        'footer' => "<br><br>--",
        'description' => t('Send HTML e-mails using PHPMailer'),
        'class' => 'Messaging_Method_Mail',
        'address_type' => 'mail',
      );
      return $info;
  }
}

/**
 * Settings form callback
 */
function messaging_phpmailer_settings_form($form_state) {
  $form['messaging_phpmailer_smtp_server'] = array(
    '#title' => t('SMTP server'),
    '#type' => 'textfield',
    '#default_value' => variable_get('messaging_phpmailer_smtp_server', ini_get('SMTP')),
  );
  $form['messaging_phpmailer_auth'] = array(
    '#type' => 'fieldset',
    '#title' => t('SMTP Authentication'),
    '#description' => t('Leave blank if your SMTP server does not require authentication.'),
  );
  $form['messaging_phpmailer_auth']['messaging_phpmailer_smtp_username'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#default_value' => variable_get('messaging_phpmailer_smtp_username', ''),
    '#description' => t('SMTP Username.'),
  );
  $form['messaging_phpmailer_auth']['messaging_phpmailer_smtp_password'] = array(
    '#type' => 'textfield',
    '#title' => t('Password'),
    '#default_value' => variable_get('messaging_phpmailer_smtp_password', ''),
    '#description' => t('SMTP password.'),
  );
  $form['messaging_phpmailer_auth']['messaging_phpmailer_smtp_secure'] = array(
    '#type' => 'radios',
    '#title' => t('SMTP Security'),
    '#options' => array(
      '' => t('None'),
      'tls' => t('TLS'),
      'ssl' => t('SSL'),
    ),
    '#default_value' => variable_get('messaging_phpmailer_smtp_secure', ''),
    '#description' => t('What security, if any, to use for this SMTP connection.'),
  );
  $form['messaging_phpmailer_auth']['messaging_phpmailer_smtp_port'] = array(
    '#type' => 'textfield',
    '#title' => t('SMTP Port'),
    '#default_value' => variable_get('messaging_phpmailer_smtp_port', 25),
    '#description' => t('What port to use for the connection.'),
  );
  $form['messaging_phpmailer_attach'] = array(
    '#title' => t('Include attachments'),
    '#type' => 'checkbox',
    '#default_value' => variable_get('messaging_phpmailer_attach', 0),
    '#description' => t('If enabled, attachments will be included with outgoing messages.'),
  );
  $form['messaging_phpmailer_bcc'] = array(
    '#title' => t('Send all messages via BCC'),
    '#type' => 'checkbox',
    '#default_value' => variable_get('messaging_phpmailer_bcc', 0),
    '#description' => t('If enabled, all messages will be sent as BCCs and will have no "To:" address. '),
  );
  $form['messaging_phpmailer_debug'] = array(
    '#title' => t('Debug mode'),
    '#type' => 'radios',
    '#options' => array(
      t('Disabled'),
      t('Enabled'),
    ),
    '#default_value' => variable_get('messaging_phpmailer_debug', 0),
    '#description' => t('If enabled, PHPMailer debugging will be activated and all messages logged to watchdog.'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_theme()
 */
function messaging_phpmailer_theme() {
  return array(
    'messaging_phpmailer' => array(
      'arguments' => array(
        'mail' => NULL,
      ),
    ),
  );
}

/**
 * Send mail message to user account. Supports bulk sending
 *
 * @param $destination
 *   Single email address
 * @param $message
 *   Message array
 */
function messaging_phpmailer_send_msg($destination, $message, $params = array()) {

  // Build the message in drupal mail format
  $mail = $message
    ->send_method()
    ->mail_build($destination, $message, $params);
  return messaging_phpmailer_drupal_mail($mail);
}

/**
 * Load the PHPMailer library.
 *
 * @return
 *   TRUE if the PHPMailer library is loaded, FALSE otherwise.
 */
function messaging_phpmailer_load_library() {
  if (!class_exists('PHPMailer', FALSE)) {

    // First, try using libraries module.
    if (module_exists('libraries')) {

      // Let's see if PHPMailer is really available from libraries.
      $phpmailer_library = './' . libraries_get_path('phpmailer') . '/class.phpmailer.php';
      if (file_exists($phpmailer_library)) {
        include_once $phpmailer_library;
      }
    }

    // If PHPMailer is not already loaded, then try from module subdirectory.
    if (!class_exists('PHPMailer', FALSE)) {
      $phpmailer_library = './' . drupal_get_path('module', 'messaging_phpmailer') . '/PHPMailer/class.phpmailer.php';
      if (file_exists($phpmailer_library)) {
        include_once $phpmailer_library;
      }
    }
  }

  // Tell the caller if PHPMailer class exists.
  return class_exists('PHPMailer', FALSE);
}

/**
 * Send a message via PHPMailer.
 * This function mimics drupal_mail. We do not use drupal_mail instead because we want
 * to be able to send mail with both PHPMailer and MIMEMail.
 */
function messaging_phpmailer_drupal_mail($message) {
  if (!messaging_phpmailer_load_library()) {
    watchdog('messaging', 'Could not locate PHPMailer library.', array(), WATCHDOG_ERROR);
    return FALSE;
  }
  $mail = new PHPMailer();
  $mail
    ->IsSMTP();

  // telling the class to use SMTP
  $mail->CharSet = 'utf-8';
  $mail->ContentType = 'text/html';
  if (variable_get('messaging_phpmailer_smtp_secure', '') != '') {
    $mail->SMTPSecure = variable_get('messaging_phpmailer_smtp_secure', '');
  }
  $mail->Port = variable_get('messaging_phpmailer_smtp_port', 25);

  // Set the authentication settings.
  $username = variable_get('messaging_phpmailer_smtp_username', '');
  $password = variable_get('messaging_phpmailer_smtp_password', '');

  // If username and password are given, use SMTP authentication.
  if ($username && $password) {
    $mail->SMTPAuth = TRUE;
    $mail->Username = $username;
    $mail->Password = $password;
  }
  $host = variable_get('messaging_phpmailer_smtp_server', ini_get('SMTP'));
  if ($host) {
    $mail->Host = $host;

    // SMTP server
  }
  else {
    watchdog('messaging', 'SMTP server cannot be reached.', array(), WATCHDOG_ERROR);
    return FALSE;
  }

  // Theme the mail message
  list($subject, $body) = theme('messaging_phpmailer', $message);

  // Add some headers. Look for rfc2822 "From" formatting (ex: "name" <foo@bar.com>)
  $matches = array();
  preg_match('/["\']?(.*)["\']? <(.*)>/', $message['headers']['From'], $matches);
  if (!empty($matches[1])) {
    $from['name'] = $matches[1];
  }
  if (!empty($matches[2])) {
    $from['email'] = $matches[2];
  }
  $matches = array();
  preg_match('/["\']?(.*)["\']? <(.*)>/', $message['headers']['Reply-To'], $matches);
  if (!empty($matches[1])) {
    $replyto['name'] = $matches[1];
  }
  if (!empty($matches[2])) {
    $replyto['email'] = $matches[2];
  }
  $mail->From = !empty($from['email']) ? $from['email'] : $message['headers']['From'];
  $mail->FromName = !empty($from['name']) ? $from['name'] : '';
  if (!empty($message['headers']['Sender'])) {
    $mail->Sender = $message['headers']['Sender'];
  }
  if (!empty($message['headers']['Errors-To'])) {
    $mail
      ->AddCustomHeader('Errors-To:' . $message['headers']['Errors-To']);
  }
  if (variable_get('messaging_phpmailer_bcc', 0)) {
    $mail
      ->AddBCC($message['to']);
  }
  else {
    $mail
      ->AddAddress($message['to']);
  }
  if (!empty($message['headers']['Message-ID'])) {
    $mail->MessageID = $message['headers']['Message-ID'];
  }
  if (!empty($replyto['name']) && !empty($replyto['email'])) {
    $mail
      ->AddReplyTo($replyto['email'], $replyto['name']);
  }

  // Strip HTML out of $body for plaintext equivalent of HTML email.
  $mail->AltBody = drupal_html_to_text($body);

  // The subject has been already filtered by messaging module
  $mail->Subject = $subject;
  $mail->Body = str_replace("\r", '', $body);
  if (!empty($message['attachments']) && variable_get('messaging_phpmailer_attach', 0)) {
    foreach ($message['attachments'] as $attachment) {
      $mail
        ->AddAttachment($attachment->filepath, $attachment->filename, 'base64', $attachment->filemime);
    }
  }

  // If enabled debug option, log everything...
  if (variable_get('messaging_phpmailer_debug', 0)) {
    $mail->SMTPDebug = TRUE;
    $result = $mail
      ->Send();
    watchdog('messaging', 'PHPMailer debug message: ' . $result . ' - ' . $mail->ErrorInfo);
  }
  else {
    $result = $mail
      ->Send();
    if (!$result) {
      watchdog('messaging', 'PHPMailer exception: !details', array(
        '!details' => $mail->ErrorInfo,
      ), WATCHDOG_ERROR);
    }
  }
  return $result;
}

/**
 * Default theme messaging_phpmailer
 */
function theme_messaging_phpmailer($mail) {
  return array(
    $mail['subject'],
    $mail['body'],
  );
}

/**
 * Implementation of hook_disable()
 */
function messaging_phpmailer_disable() {
  messaging_method_disable('phpmailer');
}

/**
 * Implementation of hook_help().
 */
function messaging_phpmailer_help($path, $arg) {
  switch ($path) {
    case 'admin/messaging/settings/method/phpmailer':
      $output = '<p>' . t('You can configure global Messaging email settings (such as sender address format) on the <a href="@mail_settings">mail settings page</a>.', array(
        '@mail_settings' => url('admin/messaging/settings/method/mail'),
      )) . '</p>';
      return $output;
  }
}

Functions

Namesort descending Description
messaging_phpmailer_disable Implementation of hook_disable()
messaging_phpmailer_drupal_mail Send a message via PHPMailer. This function mimics drupal_mail. We do not use drupal_mail instead because we want to be able to send mail with both PHPMailer and MIMEMail.
messaging_phpmailer_help Implementation of hook_help().
messaging_phpmailer_load_library Load the PHPMailer library.
messaging_phpmailer_menu Implementation of hook_menu().
messaging_phpmailer_messaging Implementation of hook_messaging
messaging_phpmailer_send_msg Send mail message to user account. Supports bulk sending
messaging_phpmailer_settings_form Settings form callback
messaging_phpmailer_theme Implementation of hook_theme()
theme_messaging_phpmailer Default theme messaging_phpmailer