Send transactional emails with Sendgrid

To use the Sendgrid integration to send transactional emails 1 environment variable is required:

Once you configured this API key as an environment variable you can use the two functions to either send plain text emails or pre-built template emails.

To send plain text emails use the following function:

import { sendPlainTextMail } from '@/utils/emails/sendgrid'

...

await sendPlainTextMail(
  'to-email@test.com',                      // to email address
  'from-email@test.com',                    // from email address - usually an email address from your company
  'Subject of email',                       // email subject
  'Email body - hey this is a test email',  // plain text body of email
  'optional-email-category'                 // email category - used for tracking open rate, clicks, analytics, etc.
)

To send an email using a pre-built Sendgrid template use the following function:

import { sendTemplateMail } from '@/utils/emails/sendgrid'

...

await sendTemplateMail(
  'to-email@test.com',                // to email address
  'from-email@test.com',              // from email address - usually an email address from your company
  'Subject of email',                 // email subject
  'sendgrid-email-template-id',       // Sendgrid dynamic template ID - e.g. d-55bcdcad24224dceb203ae3a1fd92db7 
  'optional-email-category',          // email category - used for tracking open rate, clicks, analytics, etc.
  {                                   // dynamic template data configured in Sendgrid dynamic templates
    propName1: 'test value',
    propName2: 'another test value',
  }
)
Last updated: