You are here

public function SendDefaultForm::submitForm in Mail Safety 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 FormInterface::submitForm

File

src/Form/SendDefaultForm.php, line 77

Class

SendDefaultForm
Class SendDefaultForm.

Namespace

Drupal\mail_safety\Form

Code

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

  // Resend the mail and bypass mail_alter by using the drupal_mail_system.
  $mail_array = $this->mailSafety['mail'];
  $mail_array['send'] = TRUE;

  // Let other modules respond before a mail is sent.
  // E.g. add attachments that were saved in the mail.
  $modules = \Drupal::moduleHandler()
    ->getImplementations('mail_safety_pre_send');
  foreach ($modules as $module) {
    $mail_array = \Drupal::moduleHandler()
      ->invoke($module, 'mail_safety_pre_send', $mail_array);
  }
  $mail_array['to'] = $form_state
    ->getValue('to');

  // Get the mail manager and the mail system because we already
  // got the e-mail during the mail function and want to skip drupal
  // parsing the mail again.
  $system = MailSafetyController::getMailSystem($mail_array);
  $mail_array = $system
    ->format($mail_array);
  $mail_array['result'] = $system
    ->mail($mail_array);
  if ($mail_array['result']) {
    $this
      ->messenger()
      ->addStatus(t('Succesfully sent the message to @to', array(
      '@to' => $mail_array['to'],
    )));
  }
  else {
    $this
      ->messenger()
      ->addError(t('Failed to send the message to @to', array(
      '@to' => $mail_array['to'],
    )));
  }
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}