You are here

private function SettingsForm::sendTestEmail in PHPMailer SMTP 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/SettingsForm.php \Drupal\phpmailer_smtp\Form\SettingsForm::sendTestEmail()
  2. 2.x src/Form/SettingsForm.php \Drupal\phpmailer_smtp\Form\SettingsForm::sendTestEmail()
  3. 2.1.x src/Form/SettingsForm.php \Drupal\phpmailer_smtp\Form\SettingsForm::sendTestEmail()

Sends a test email.

Parameters

array $values: Array of form values.

1 call to SettingsForm::sendTestEmail()
SettingsForm::submitForm in src/Form/SettingsForm.php
Form submission handler.

File

src/Form/SettingsForm.php, line 367

Class

SettingsForm
Defines a form to configure PHPMailer SMTP settings.

Namespace

Drupal\phpmailer_smtp\Form

Code

private function sendTestEmail(array $values) {

  // Since this is being sent to an email address that may not necessarily
  // be tied to a user account, use the site's default language.
  $langcode = $this->languageManager
    ->getDefaultLanguage()
    ->getId();

  // Prepare the message without sending.
  $message = $this->mailManager
    ->mail('phpmailer_smtp', 'test', $values['phpmailer_smtp_test'], $langcode, [], NULL, FALSE);
  $message['subject'] = (string) $this
    ->t('PHPMailer SMTP test email');
  $message['body'] = (string) $this
    ->t('Your site is properly configured to send emails using the PHPMailer library.');

  // Send the message using PHPMailer SMTP.
  $phpMailerSmtp = $this->mailManager
    ->createInstance('phpmailer_smtp');
  $phpMailerSmtp
    ->mail($message);

  // Some users may not have the dblog module enabled.
  if ($this->moduleHandler
    ->moduleExists('dblog')) {
    $watchdog_url = Link::createFromRoute($this
      ->t('Check the logs'), 'dblog.overview');
    $this
      ->messenger()
      ->addMessage($this
      ->t('A test e-mail has been sent to %email. @watchdog-url for any error messages.', [
      '%email' => $values['phpmailer_smtp_test'],
      '@watchdog-url' => $watchdog_url
        ->toString(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage($this
      ->t('A test e-mail has been sent to %email.', [
      '%email' => $values['phpmailer_smtp_test'],
    ]));
  }
}