You are here

public function TestMailForm::submitForm in Sparkpost email 8

Same name and namespace in other branches
  1. 8.2 src/Form/TestMailForm.php \Drupal\sparkpost\Form\TestMailForm::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/TestMailForm.php, line 93

Class

TestMailForm
Class TestMailForm.

Namespace

Drupal\sparkpost\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  try {
    $to = $form_state
      ->getValue('recipient');
    $subject = $form_state
      ->getValue('subject');
    $message = $form_state
      ->getValue('body');
    $response = $this->sparkpostClient
      ->sendMessage([
      'content' => [
        'subject' => $subject,
        'html' => $message,
        'text' => MailFormatHelper::htmlToText($message),
      ],
      'recipients' => [
        [
          'address' => [
            'email' => $to,
          ],
        ],
      ],
    ]);
    drupal_set_message($this
      ->t('Sparkpost test email sent from %to.', [
      '%to' => $to,
    ]), 'status');
  } catch (\Exception $e) {
    drupal_set_message($this
      ->t('Message could not be sent: %message', [
      '%message' => $e
        ->getMessage(),
    ]), 'error');
  }
}