You are here

public function SMTPConfigForm::submitForm in SMTP Authentication Support 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/SMTPConfigForm.php, line 397

Class

SMTPConfigForm
Implements the SMTP admin settings form.

Namespace

Drupal\smtp\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $config = $this->configFactory
    ->getEditable('smtp.settings');
  $mail_config = $this->configFactory
    ->getEditable('system.mail');
  $mail_system = $mail_config
    ->get('interface.default');

  // Updating config vars.
  if (isset($values['smtp_password']) && !$this
    ->isOverridden('smtp_password')) {
    $config
      ->set('smtp_password', $values['smtp_password']);
  }
  if (!$this
    ->isOverridden('smtp_on')) {
    $config
      ->set('smtp_on', $values['smtp_on'] == 'on')
      ->save();
  }
  if (!$this
    ->isOverridden('smtp_autotls')) {
    $config
      ->set('smtp_autotls', $values['smtp_autotls'] == 'on')
      ->save();
  }
  $config_keys = [
    'smtp_host',
    'smtp_hostbackup',
    'smtp_port',
    'smtp_protocol',
    'smtp_timeout',
    'smtp_username',
    'smtp_from',
    'smtp_fromname',
    'smtp_client_hostname',
    'smtp_client_helo',
    'smtp_allowhtml',
    'smtp_test_address',
    'smtp_reroute_address',
    'smtp_debugging',
    'smtp_keepalive',
  ];
  foreach ($config_keys as $name) {
    if (!$this
      ->isOverridden($name)) {
      $config
        ->set($name, $values[$name])
        ->save();
    }
  }

  // Set as default mail system if module is enabled.
  if ($config
    ->get('smtp_on') || $this
    ->isOverridden('smtp_on') && $values['smtp_on'] == 'on') {
    if ($mail_system != 'SMTPMailSystem') {
      $config
        ->set('prev_mail_system', $mail_system);
    }
    $mail_system = 'SMTPMailSystem';
    $mail_config
      ->set('interface.default', $mail_system)
      ->save();
  }
  else {
    $default_system_mail = 'php_mail';
    $mail_config = $this->configFactory
      ->getEditable('system.mail');
    $default_interface = $mail_config
      ->get('prev_mail_system') ? $mail_config
      ->get('prev_mail_system') : $default_system_mail;
    $mail_config
      ->set('interface.default', $default_interface)
      ->save();
  }

  // If an address was given, send a test e-mail message.
  if ($test_address = $values['smtp_test_address']) {
    $params['subject'] = $this
      ->t('Drupal SMTP test e-mail');
    $params['body'] = [
      $this
        ->t('If you receive this message it means your site is capable of using SMTP to send e-mail.'),
    ];

    // If module is off, send the test message
    // with SMTP by temporarily overriding.
    if (!$config
      ->get('smtp_on')) {
      $original = $mail_config
        ->get('interface');
      $mail_system = 'SMTPMailSystem';
      $mail_config
        ->set('interface.default', $mail_system)
        ->save();
    }
    if ($this->mailManager
      ->mail('smtp', 'smtp-test', $test_address, $this->currentUser
      ->getPreferredLangcode(), $params)) {
      $this->messenger
        ->addMessage($this
        ->t('A test e-mail has been sent to @email via SMTP. You may want to check the log for any error messages.', [
        '@email' => $test_address,
      ]));
    }
    if (!$config
      ->get('smtp_on')) {
      $mail_config
        ->set('interface', $original)
        ->save();
    }
  }
  parent::submitForm($form, $form_state);
}