You are here

public function MailgunTestEmailForm::submitForm in Mailgun 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/MailgunTestEmailForm.php, line 164

Class

MailgunTestEmailForm
Provides test email form with common email parameters.

Namespace

Drupal\mailgun\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $to = $form_state
    ->getValue('to');
  $params = [
    'subject' => $this
      ->t('Mailgun works!'),
    'body' => [
      $form_state
        ->getValue('body'),
    ],
  ];
  if (!empty($form_state
    ->getValue('include_attachment'))) {
    $params['attachments'][] = [
      'filepath' => $this->fileSystem
        ->realpath('core/misc/druplicon.png'),
    ];
  }

  // Add CC / BCC values if they are set.
  if (!empty($cc = $form_state
    ->getValue('cc'))) {
    $params['cc'] = $cc;
  }
  if (!empty($bcc = $form_state
    ->getValue('bcc'))) {
    $params['bcc'] = $bcc;
  }
  $result = $this->mailManager
    ->mail('mailgun', 'test_form_email', $to, $this->user
    ->getPreferredLangcode(), $params, $form_state
    ->getValue('reply_to'), TRUE);
  if (!empty($result)) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Successfully sent message to %to.', [
      '%to' => $to,
    ]));
  }
  else {
    if ($this->moduleHandler
      ->moduleExists('dblog')) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Something went wrong. Please check @logs for details.', [
        '@logs' => Link::createFromRoute($this
          ->t('logs'), 'dblog.overview')
          ->toString(),
      ]), 'warning');
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Something went wrong. Please check logs for details.'), 'warning');
    }
  }
}