Skip to content

Commit

Permalink
nys-155: implement hook_mail_alter to pass the registration email thr…
Browse files Browse the repository at this point in the history
…ough a sendgrid template
  • Loading branch information
nathanielwoodland committed Jun 26, 2024
1 parent 4d45870 commit 49238f6
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions web/modules/custom/nys_registration/nys_registration.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use SendGrid\Mail\From;
use SendGrid\Mail\Mail;
use SendGrid\Mail\Personalization;
use SendGrid\Mail\To;

/**
* Implements hook_entity_type_alter().
Expand Down Expand Up @@ -122,3 +126,54 @@ function nys_registration_user_login_form_submit($form, FormStateInterface $form
$form_state->setRedirectUrl(Url::fromUri($url));
}
}

/**
* Implements hook_mail_alter().
*/
function nys_registration_mail_alter(&$message) {
if ($message['key'] ?? '' === 'register_no_approval_required') {
// Build the \SendGrid\Mail\Mail object.
try {
$from_mail = $message['from'] ?? '';
$from = new From($from_mail, $from_mail);
$mail = new Mail($from);

// Set the SendGrid template ID.
// @todo replace with new template ID.
$mail->setTemplateId('5182d9b2-8670-41cb-9f65-5f671541907f');

// Add the email variables.
$personalization = new Personalization();
/** @var \Drupal\user\Entity\User $account */
$account = $message['params']['account'] ?? NULL;
$user_first_name = $account?->field_first_name?->value;
$user_reset_link = user_pass_reset_url($account);
// @todo replace with new vars.
$substitutions = [
'%bill.base_print%' => $user_first_name,
'%bill.session%' => $user_first_name,
'%bill.print_number%' => $user_first_name,
'%bill.chamber%' => $user_first_name,
'%bill.summary%' => $user_first_name,
'%bill.sponsor%' => $user_first_name,
'%confirm_url%' => $user_reset_link,
];
foreach ($substitutions as $s_key => $s_val) {
$personalization->addSubstitution($s_key, $s_val);
}
$mail->addPersonalization($personalization);
}
catch (Throwable $e) {
\Drupal::messenger()
->addError('There was an error sending the registration email. Please contact a site admin.');
\Drupal::logger('nys_registration')
->error('Failed to build \SendGrid\Mail\Mail object for mail with key: register_no_approval_required.');
$message['send'] = FALSE;
return;
}

// Attach Mail object to $messages array, so that the email gets sent using
// SendGrid template.
$message['params']['sendgrid_mail'] = $mail;
}
}

0 comments on commit 49238f6

Please sign in to comment.