You are here

function sendgrid_integration_test_submit in SendGrid Integration 8

Same name and namespace in other branches
  1. 8.2 sendgrid_integration.admin.inc \sendgrid_integration_test_submit()
  2. 7 sendgrid_integration.admin.inc \sendgrid_integration_test_submit()

Implements hook_submit().

File

./sendgrid_integration.admin.inc, line 85
Provides a form to send a test email through Sendgrid.

Code

function sendgrid_integration_test_submit($form, &$form_state) {

  // Create defaults array.
  $defaults = [
    'to' => $form_state['values']['to'],
    'subject' => $form_state['values']['subject'],
    'body' => $form_state['values']['body'],
    'toname' => $form_state['values']['toname'],
    'fromname' => $form_state['values']['fromname'],
    'replyto' => $form_state['values']['replyto'],
  ];

  // Set the defaults for reuse.
  variable_set('sendgrid_integration_test_defaults', $defaults);
  $params = [
    'subject' => $form_state['values']['subject'],
    'body' => check_markup($form_state['values']['body']['value'], $form_state['values']['body']['format']),
  ];
  if (!empty($form_state['values']['replyto'])) {
    $params['Reply-To'] = $form_state['values']['replyto'];
  }
  if (!empty($form_state['values']['toname'])) {
    $sendto = $form_state['values']['toname'] . '<' . $form_state['values']['to'] . '>';
  }
  else {
    $sendto = $form_state['values']['to'];
  }
  $params['include_test_attachment'] = $form_state['values']['include_attachment'];

  // Setting a specific mail system for the SendGrid Integration Module.
  mailsystem_set([
    'sendgrid_integration' => 'SendGridMailSystem',
  ]);

  // Attempt to send the email and post a message if it was successful.
  if (!empty($form_state['values']['fromname'])) {
    $senderfrom = $form_state['values']['fromname'] . ' <' . variable_get('site_mail') . '>';
  }
  else {
    $senderfrom = variable_get('site_mail');
  }
  $result = drupal_mail('sendgrid_integration', 'test', $sendto, language_default(), $params, $senderfrom);
  if (isset($result['result']) && $result['result'] == 'true') {
    \Drupal::messenger()
      ->addStatus(t('SendGrid test email sent from %from to %to.', [
      '%from' => $senderfrom,
      '%to' => $sendto,
    ]));
  }
}