Using dynamic templates

Created on 28 February 2023, over 1 year ago

Problem/Motivation

We wanted to push all communications through a dynamic template, and it turned out to be pretty easy, just creating a plugin and extending the existing SendGridMail Plugin class.

πŸ“Œ Task
Status

Active

Version

1.1

Component

Documentation

Created by

πŸ‡¦πŸ‡ΊAustralia singularo Adelaide, AUS

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @singularo
  • πŸ‡¦πŸ‡ΊAustralia singularo Adelaide, AUS
    • Create a Dynamic template in the SendGrid UI with fields to match those in the $this->mail->addSubstitutions() call
    • Create a simple module of your own or use an existing one
    • In the src/Plugin/Mail directory, create a new class like the example below, which extends the SendGridMail class
    • Enable the module
    • Select the new plugin in the mailsystem page
    • Send test email to confirm all working

    Example class:

    <?php
    
    namespace Drupal\your_module\Plugin\Mail;
    
    use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
    use Drupal\sendgrid_mailer\Plugin\Mail\SendGridMail;
    use SendGrid\Mail\Mail;
    
    /**
     * Defines the default Drupal mail backend, using SendGrid API.
     *
     * @Mail(
     *   id = "custom_sendgrid_mail",
     *   label = @Translation("Custom SendGrid mailer"),
     *   description = @Translation("Sends emails using SendGrid API")
     * )
     */
    class CustomSendGridMail extends SendGridMail implements ContainerFactoryPluginInterface {
    
      /**
       * {@inheritdoc}
       */
      public function format(array $message) {
        if (is_array($message['body'])) {
          $message['body'] = implode("\n\n", $message['body']);
        }
    
        return $message;
      }
    
      /**
       * {@inheritdoc}
       */
      public function mail(array $message) {
        try {
          $this->mail = new Mail();
    
          $this->setFrom($message);
    
          $this->addHeaders($message);
    
          $this->addRecipients($message);
    
          $this->mail->setSubject($message['subject']);
          $this->mail->setTemplateId('<template id from sendgrid>');
          $this->mail->addSubstitutions([
            'name' => $message['params']['account']->getDisplayName(),
            'subject' => $message['subject'],
            'notification_message' => $message['body'],
          ]);
    
          $this->addAttachments($message);
    
          $response = $this->sendGrid->client->mail()->send()->post($this->mail);
          if ($response->statusCode() !== 202) {
            watchdog_exception('sendgrid_mailer', new \Exception('SendGrid Error'), $response->body());
          }
    
          return $response->statusCode() === 202;
        }
        catch (\Exception $e) {
          watchdog_exception('sendgrid_mailer', $e, $e->getMessage());
        }
    
        return FALSE;
      }
    
    }
    
Production build 0.69.0 2024