Mailer
Overview
The Mailer package is a utility designed to facilitate the process of generating email templates. This package provides a way to style, template, and generate HTML for sending emails using the React Email
library
Template System
The template system is key to the functionality of the Mailer package. Each template corresponds to a different type of email that can be sent. The specific variables available for substitution in a template depend on the template itself.
The Template type should be used to reference the available templates. For example, to reference to verify email template, you would use Template.VERIFY_EMAIL
.
Adding new Template
All development takes place in the package turborepo packages/mailer
. The preview of emails is launched automatically when you launch the application using turborepo or Docker, the application will be available on port 3003.
- Create a new template in the
emails
folder. - Export your component and props interface from the file.
- Navigate to
src/template.ts
file, import your component and props interface. - Add your template to the
Template
enum,EmailComponent
object andTemplateProps
type.
When you change the email template, you need to rebuild the API to see the changes when sending emails.
Usage in API
Importing
To use the Mailer package within the API, the necessary services and types need to be imported:
Sending an Email
To send an email, the sendTemplate
function from the emailService
is used. This function accepts an object with properties specifying the recipient, subject line, template to be used, and an object with parameters to be filled into the template:
In this example, a password reset email is dispatched. The VERIFY_EMAIL
template is used, and the user’s first name and the reset password URL are passed as parameters to be substituted into the template.