Mailer
Here you will learn how our mail service built.
Currently we can have one of two email services. Both required additional packages installed, .env setup, also you must register in their services. They have pretty similar approach. They have their own documentation with this service, so we will not repeat ourselves here, but just consider how it works for us.
AWS
At this link you will read all the necessary information about how to install AWS into our project. Here we just mentioned about AWS email service.
Here you can read the official documentation about their email service.
Before we start you must set the .env constants with you AWS SES User Credentials:
EMAIL_SERVICE
EMAIL_SENDER
AWS_SES_REGION
AWS_SES_ACCESS_KEY
AWS_SES_SECRET_ACCESS_KEY
SendGrid
Same all for SendGrid. Just follow their guide from documentation.
.env:
EMAIL_SERVICE
EMAIL_SENDER
SENDGRID_API_KEY
N.B. if you cannot find such constants in .env file, than it can be found in .../src/constants.ts. This was done within the security framework if somewhere something becomes unavailable.
N.B. EMAIL_SERVICE and EMAIL_SENDER are the place were we specify what kind of service we will use. This constants are common to both services.
Structure
Pretty simple and common form for Nest.JS. Email have it's own module where all the magic happens.
...api/src/email/
But there you can find another folder for AWS and SendGrid services separately, email.service.ts uses one of those.
In .../api/src/common/interfaces/ you will find interfaces for email templates.
Email templates lays in .../api/email-templates/
All this must done/created before sending email. After you do this, just add the method into the service file and use it where you need it. For example invitation email that we've mentioned in Team Management.
Here you can find event two different emails that we send in two different situations.
Conclusions
Register a user account on one or both services.
Install all the necessary packages.
Setup .env file and constants.ts
Create email templates, interfaces for that templates and module for email service.
Use email method where you need.
Last updated