You are here

public function PhpMailerSmtp::smtpSend in PHPMailer SMTP 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Mail/PhpMailerSmtp.php \Drupal\phpmailer_smtp\Plugin\Mail\PhpMailerSmtp::smtpSend()
  2. 2.x src/Plugin/Mail/PhpMailerSmtp.php \Drupal\phpmailer_smtp\Plugin\Mail\PhpMailerSmtp::smtpSend()
  3. 2.1.x src/Plugin/Mail/PhpMailerSmtp.php \Drupal\phpmailer_smtp\Plugin\Mail\PhpMailerSmtp::smtpSend()

Send mail via SMTP.

Wrapper around PHPMailer::SmtpSend() with exception handling.

File

src/Plugin/Mail/PhpMailerSmtp.php, line 181

Class

PhpMailerSmtp
Implements the base PHPMailer SMTP class for the Drupal MailInterface.

Namespace

Drupal\phpmailer_smtp\Plugin\Mail

Code

public function smtpSend($header, $body) {
  if ($this->SMTPDebug) {

    // Clear possibly previously captured debug output.
    $this->drupalDebugOutput = '';
    ob_start();
  }
  try {
    $result = parent::SmtpSend($header, $body);

    // Close connection when not using SMTP keep-alive.
    if (!$this->SMTPKeepAlive) {
      $this
        ->SmtpClose();
    }
  } catch (Exception $exception) {
  }
  if ($this->SMTPDebug) {
    if ($this->drupalDebug && ($this->drupalDebugOutput = ob_get_contents())) {
      $this->messenger
        ->addMessage(Markup::create($this->drupalDebugOutput));
      if ($this->config
        ->get('smtp_debug_log', 0)) {
        $this->loggerFactory
          ->get('phpmailer_smtp')
          ->debug('Output of communication with SMTP server:<br /><pre>{output}</pre>', [
          'output' => str_replace("<br>\n", "\n", Html::decodeEntities($this->drupalDebugOutput)),
        ]);
      }
    }
    ob_end_clean();
  }

  // Reinitialize properties.
  $this
    ->reset();
  if (isset($exception)) {

    // Pass exception to caller.
    throw $exception;
  }
  return $result;
}