You are here

protected function ConnectionTester::configurePhpMailer in SMTP Authentication Support 8

Get a PHPMailer object ready to be tested.

Return value

bool True if config was set, False if phpMailer didn't exist.

1 call to ConnectionTester::configurePhpMailer()
ConnectionTester::testConnection in src/ConnectionTester/ConnectionTester.php
Test SMTP connection.

File

src/ConnectionTester/ConnectionTester.php, line 166

Class

ConnectionTester
Allows testing the SMTP connection.

Namespace

Drupal\smtp\ConnectionTester

Code

protected function configurePhpMailer() {
  if ($this->phpMailer) {

    // Set debug to FALSE for the connection test; further debugging can be
    // used when sending actual mails.
    $this->phpMailer->SMTPDebug = FALSE;

    // Hardcoded Timeout for testing so the reports page doesn't stall out.
    $this->phpMailer->Timeout = 5;
    $this->phpMailer->Host = implode(';', array_filter([
      $this->smtpConfig
        ->get('smtp_host'),
      $this->smtpConfig
        ->get('smtp_hostbackup'),
    ]));
    $this->phpMailer->Port = $this->smtpConfig
      ->get('smtp_port');
    $protocol = $this->smtpConfig
      ->get('smtp_protocol');
    $this->phpMailer->SMTPAutoTLS = $this->smtpConfig
      ->get('smtp_autotls');
    $this->phpMailer->SMTPSecure = in_array($protocol, [
      'ssl',
      'tls',
    ], TRUE) ? $protocol : '';
    if ($smtp_client_hostname = $this->smtpConfig
      ->get('smtp_client_hostname')) {
      $this->phpMailer->Hostname = $smtp_client_hostname;
    }
    if ($helo = $this->smtpConfig
      ->get('smtp_client_helo')) {
      $this->phpMailer->Helo = $helo;
    }
    $username = $this->smtpConfig
      ->get('smtp_username');
    $password = $this->smtpConfig
      ->get('smtp_password');
    if ($username && $password) {
      $this->phpMailer->SMTPAuth = TRUE;
      $this->phpMailer->Username = $username;
      $this->phpMailer->Password = $password;
    }
    return TRUE;
  }
  return FALSE;
}