You are here

public function HtmlMailTestForm::submitForm in HTML Mail 8

Same name and namespace in other branches
  1. 8.3 src/Form/HtmlMailTestForm.php \Drupal\htmlmail\Form\HtmlMailTestForm::submitForm()

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 FormInterface::submitForm

File

src/Form/HtmlMailTestForm.php, line 159

Class

HtmlMailTestForm
Class HtmlMailTestForm.

Namespace

Drupal\htmlmail\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Get the form values.
  $defaults = [
    'to' => $form_state
      ->getValue('to'),
    'subject' => $form_state
      ->getValue('subject'),
    'body' => $form_state
      ->getValue('body'),
    'class' => $form_state
      ->getValue('class'),
  ];

  // Set the defaults for reuse.
  $config = $this
    ->configFactory()
    ->getEditable('htmlmail.settings');
  $config
    ->set('htmlmail_test', $defaults)
    ->save();

  // Send the email.
  $params = [
    'subject' => $defaults['subject'],
    'body' => check_markup($defaults['body']['value'], $defaults['body']['format']),
  ];

  // Send the email.
  $langcode = $this->accountInterface
    ->getPreferredLangcode();
  $config = $this
    ->configFactory()
    ->getEditable('mailsystem.settings');
  $config
    ->set('defaults.sender', $defaults['class'])
    ->set('defaults.formatter', $defaults['class'])
    ->save();
  $result = $this->mailManager
    ->mail(HtmlMailHelper::getModuleName(), self::KEY_NAME, $defaults['to'], $langcode, $params, NULL, TRUE);
  if ($result['result'] === TRUE) {
    drupal_set_message($this
      ->t('HTML Mail test message sent.'));
  }
  else {
    drupal_set_message($this
      ->t('Something went wrong. Please check @logs for details.', [
      '@logs' => Link::createFromRoute($this
        ->t('logs'), 'dblog.overview')
        ->toString(),
    ]), 'error');
  }
}