You are here

public function SettingsForm::submitForm in PHPMailer 8.3

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/SettingsForm.php, line 267

Class

SettingsForm
Defines a form that configures devel settings.

Namespace

Drupal\phpmailer\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();

  // Check to see if the module has been activated or inactivated.
  if ($values['smtp_on']) {
    if (!phpmailer_active()) {

      // This module is inactive and is being activated.

      /**
       * @todo This part needs to be figured out.
       */

      //        $mailsystem_config = $this->config('mailsystem.settings');
      $mail_config = $this->configFactory
        ->getEditable('system.mail');
      $mail_system = $mail_config
        ->get('interface.default');
      if ($mail_system != 'phpmailer') {
        $mail_system = 'phpmailer';
        $mail_config
          ->set('interface.default', $mail_system)
          ->save();
      }
      drupal_set_message(t('PHPMailer will be used to deliver all site e-mails.'));
      \Drupal::logger('phpmailer')
        ->notice('PHPMailer has been enabled.');
    }
  }
  elseif (phpmailer_active()) {

    // This module is active and is being inactivated.

    /**
     * @todo This part needs to be figured out.
     */

    //      $mailsystem_config = $this->config('mailsystem.settings');
    $mail_config = $this->configFactory
      ->getEditable('system.mail');
    $mail_system = $mail_config
      ->get('interface.default');
    if ($mail_system == 'phpmailer') {
      $mail_system = 'php_mail';
      $mail_config
        ->set('interface.default', $mail_system)
        ->save();
    }
    drupal_set_message(t('PHPMailer has been disabled.'));
    \Drupal::logger('phpmailer')
      ->notice('PHPMailer has been disabled.');
  }

  // Save the configuration changes.
  $phpmailer_config = $this
    ->config('phpmailer.settings');
  $phpmailer_config
    ->set('smtp_on', $values['smtp_on'])
    ->set('smtp_host', $values['smtp_host'])
    ->set('smtp_hostbackup', $values['smtp_hostbackup'])
    ->set('smtp_port', $values['smtp_port'])
    ->set('smtp_protocol', $values['smtp_protocol'])
    ->set('smtp_ssl_verify_peer', $values['smtp_ssl_verify_peer'])
    ->set('smtp_ssl_verify_peer_name', $values['smtp_ssl_verify_peer_name'])
    ->set('smtp_ssl_allow_self_signed', $values['smtp_ssl_allow_self_signed'])
    ->set('smtp_username', $values['smtp_username'])
    ->set('smtp_fromname', $values['smtp_fromname'])
    ->set('smtp_always_replyto', $values['smtp_always_replyto'])
    ->set('smtp_keepalive', $values['smtp_keepalive'])
    ->set('smtp_debug', $values['smtp_debug'])
    ->set('smtp_debug_log', $values['smtp_debug_log']);

  // Only save the password, if it is not empty.
  if (!empty($values['smtp_password'])) {
    $phpmailer_config
      ->set('smtp_password', $values['smtp_password']);
  }
  $phpmailer_config
    ->save();

  /**
   * @todo This part needs to be figured out.
   */

  // Send a test email message, if an email address was entered.
  if ($values['phpmailer_test']) {

    // Since this is being sen 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();

    // If PHPMailer is enabled, send via regular drupal_mail().
    if (phpmailer_active()) {

      //        $this->mailManager->mail('phpmailer', 'test', $values['phpmailer_test'], $language_code, $params, $from, $send_now);
      \Drupal::service('plugin.manager.mail')
        ->mail('phpmailer', 'test', $values['phpmailer_test'], $langcode);
    }
    else {

      // Prepare the message without sending.
      $message = \Drupal::service('plugin.manager.mail')
        ->mail('phpmailer', 'test', $values['phpmailer_test'], $langcode, [], NULL, FALSE);

      // Send the message.
      module_load_include('inc', 'phpmailer', 'includes/phpmailer.drupal');
      $ret_val = phpmailer_send($message);
    }
    $watchdog_url = Url::fromRoute('dblog.overview');
    $watchdog_url = \Drupal::l(t('Check the logs'), $watchdog_url);
    drupal_set_message(t('A test e-mail has been sent to %email. @watchdog-url for any error messages.', [
      '%email' => $values['phpmailer_test'],
      '@watchdog-url' => $watchdog_url,
    ]));
  }
  parent::submitForm($form, $form_state);
}