You are here

phpmailer.install in PHPMailer 7.3

PHPMailer installation functions.

File

phpmailer.install
View source
<?php

/**
 * @file
 * PHPMailer installation functions.
 */

/**
 * Implements hook_requirements().
 */
function phpmailer_requirements($phase) {
  $requirements = array();

  // Ensure translations don't break at install time.
  $t = get_t();
  if ($phase == 'runtime' && phpmailer_library_exists()) {
    $mail = new PHPMailer();
    $required_version = '5.2.26';
    $installed_version = $mail->Version;
    $requirements['phpmailer'] = array(
      'title' => $t('PHPMailer library'),
      'value' => $installed_version,
    );
    if (!version_compare($installed_version, $required_version, '>=')) {
      $path = 'sites/all/libraries/phpmailer';
      $requirements['phpmailer']['severity'] = REQUIREMENT_ERROR;
      $requirements['phpmailer']['description'] = $t('PHPMailer @version or higher is required. Please <a href="@url">download the new version</a>, extract the archive and copy the contents to the following location:<br /><code>@path</code>. Make sure the main file, class.phpmailer.php, is located at<br /><code>@class</code>.', array(
        '@version' => $required_version,
        '@url' => 'https://github.com/PHPMailer/PHPMailer/tags',
        '@path' => $path,
        '@class' => $path . '/class.phpmailer.php',
      ));
    }
    else {
      $requirements['phpmailer']['severity'] = REQUIREMENT_OK;
    }
  }
  else {
    drupal_load('module', 'libraries');
    $path = libraries_get_path('phpmailer');
    if (!$path || !file_exists(DRUPAL_ROOT . '/' . $path . '/class.phpmailer.php') || !file_exists(DRUPAL_ROOT . '/' . $path . '/class.smtp.php')) {

      // Since Libraries 2.x, $path is FALSE if the library does not exist.
      $path = 'sites/all/libraries/phpmailer';
      $requirements['phpmailer'] = array(
        'title' => $t('PHPMailer library'),
        'value' => $t('Missing'),
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('Please download <a href="@url">PHPMailer for PHP5/6</a>, extract the archive and copy the contents to the following location:<br /><code>@path</code>. Make sure the main file, class.phpmailer.php, is located at<br /><code>@class</code>.', array(
          '@url' => 'https://github.com/PHPMailer/PHPMailer/tags',
          '@path' => $path,
          '@class' => $path . '/class.phpmailer.php',
        )),
      );
    }
  }
  return $requirements;
}

/**
 * Implements hook_enable().
 */
function phpmailer_enable() {
  if (!phpmailer_enabled() && !(module_exists('mimemail') && variable_get('mimemail_engine', 'mimemail') == 'phpmailer')) {
    $t = get_t();
    drupal_set_message($t('PHPMailer has been installed, but is currently disabled. <a href="@settings-url">Configure it now</a>.', array(
      '@settings-url' => url('admin/config/system/phpmailer'),
    )));
  }
  if (module_exists('mailsystem')) {
    mailsystem_set(array(
      'phpmailer' => 'DrupalPHPMailer',
    ));
  }
}

/**
 * Implements hook_disable().
 */
function phpmailer_disable() {
  if (phpmailer_enabled()) {

    // Remove PHPMailer from all mail keys it is configured for.
    $mail_system = variable_get('mail_system', array(
      'default-system' => 'DefaultMailSystem',
    ));
    foreach ($mail_system as $key => $class) {
      if ($class == 'DrupalPHPMailer') {
        if ($key != 'default-system') {
          unset($mail_system[$key]);
        }
        else {
          $mail_system[$key] = 'DefaultMailSystem';
        }
      }
    }
    variable_set('mail_system', $mail_system);
    variable_del('smtp_on');
    drupal_set_message(t('PHPMailer has been disabled.'));
  }
  if (module_exists('mimemail') && variable_get('mimemail_engine', 'mimemail') == 'phpmailer') {
    variable_del('mimemail_engine');
    drupal_set_message(t('MimeMail e-mail engine has been reset to default.'), 'warning');
  }
  if (module_exists('mailsystem')) {
    mailsystem_clear(array(
      'phpmailer' => 'DrupalPHPMailer',
    ));
  }
}

/**
 * Implements hook_uninstall().
 */
function phpmailer_uninstall() {
  $mail_system = variable_get('mail_system', array(
    'default-system' => 'DefaultMailSystem',
  ));
  $mail_system['default-system'] = 'DefaultMailSystem';
  variable_set('mail_system', $mail_system);
  variable_del('smtp_always_replyto');
  variable_del('smtp_auth_type');
  variable_del('smtp_debug');
  variable_del('smtp_debug_log');
  variable_del('smtp_encode_headers');
  variable_del('smtp_fromname');
  variable_del('smtp_hide_password');
  variable_del('smtp_host');
  variable_del('smtp_hostbackup');
  variable_del('smtp_keepalive');
  variable_del('smtp_on');
  variable_del('smtp_password');
  variable_del('smtp_port');
  variable_del('smtp_protocol');
  variable_del('smtp_username');
  variable_del('ssl_verify_peer');
  variable_del('ssl_verify_peer_name');
  variable_del('ssl_allow_self_signed');
}

/**
 * Enable PHPMailer as default mail system if it has been before.
 */
function phpmailer_update_7000() {

  // @todo Check whether the library exists?
  if (strpos(variable_get('smtp_library', ''), 'phpmailer')) {
    $mail_system = variable_get('mail_system', array(
      'default-system' => 'DefaultMailSystem',
    ));
    $mail_system['default-system'] = 'DrupalPHPMailer';
    variable_set('mail_system', $mail_system);

    // PHPMailer was the default mail engine, so it should be safe to remove the
    // variable.
    variable_del('smtp_library');
  }
}

/**
 * Add support for the Mail System module.
 */
function phpmailer_update_7001() {
  if (module_exists('mailsystem')) {
    mailsystem_set(array(
      'phpmailer' => 'DrupalPHPMailer',
    ));
  }
}

Functions

Namesort descending Description
phpmailer_disable Implements hook_disable().
phpmailer_enable Implements hook_enable().
phpmailer_requirements Implements hook_requirements().
phpmailer_uninstall Implements hook_uninstall().
phpmailer_update_7000 Enable PHPMailer as default mail system if it has been before.
phpmailer_update_7001 Add support for the Mail System module.