In this guide weâre going to use the Payload email adapter to allow our application to send emails, focusing on two main options: SendGrid with Nodemailer, and Resend.
At the time of this recording, these are the two official email adapters that Payload supports.
The Nodemailer adapter is certainly the easiest migration path if you already had email set up in a previous project. This allows you to use any Nodemailer-transport like SMTP, Resend, and SendGrid.
Most email providers should be available using this option.
The second option is Resend, which uses the Resend REST API to send emails. This is lightweight compared to Nodemailer and may be preferred if youâre using a serverless platform like Vercel.
Weâll cover each of these in detail.
Both adapters need to be imported from their respective packages into your Payload config as follows:
It should be noted that both of these packages do need to be installed via your console before we can get started as follows:npm install @payloadcms/plugin-email-nodemailer @payloadcms/email-resend
And while you could use multiple email adapters for different use cases, weâll focus on just getting each of these up and running separately as single options.
Since we're going to focus first on Nodemailer, be sure you comment out Resend first in your config.
Once imported, you will need to add the email option to your Payload config if you havenât already.
Now, letâs set up a SendGrid account and get this working.
First, youâll go to sendgrid.com and click âStart for free.â
Once youâre signed in and verified, youâll see a dashboard with some next steps.
You should follow the instructions on how to get your mail exchange servers set up in your DNS, which will prevent your emails from ending up in spam.
On the left side, you can select âEmail API,â then "Integration Guide.â Once there, youâll have two options, âWeb APIâ or âSMTP Relay.â Choose SMTP Relay, and youâll see a screen that asks you to create an API key. Name it and click âCreate Key.â This will show you an API key that youâll want to copy and paste right away into your .env
file under the SMTP_PASS
environment variable (example below).
Next, you will be able to gather the âhost,â âport,â and âuserâ options you need. Optionally include these all as SMTP_HOST
, SMTP_PORT
, and SMTP_USER
in your .env
file and assign them to your host, port, and user options.
In our case, weâre going to use port 465
, which is for secure connections.
You can then check that youâve updated your settings and verify your integration if youâd like. Either way, youâll want to test this functionality by using the âForgot passwordâ link on your Payload login page.
Now that you have your SendGrid environment variables in place, you can copy them over to your Payload config.
In order to test this, you still need to set up your sender identity within SendGrid. This is done easily by clicking 'Create sender identity' and fill out the fields therein. Again, this is for spam laws, so it is required.
Once that is done, you'll be able to verify your sender within SendGrid.
Finally, spin up your server again, click 'Forgot password?', enter the email address you'd like to test (make sure it's an existing user on your project), and verify the email has been sent.
Your other email option is the Resend configuration.
Youâll set this up much the same way as you did your Nodemailer adapter. Weâll comment out the Nodemailer adapter information and add a new email option with resendAdapter()
assigned to it.
Then, open an empty object and we can get started.
First, go to resend.com and click âGet Started.â Create a new Resend account if you donât already have one. Once confirmed and logged in, you should be brought to an onboarding page. If not, go to resend.com/onboarding.
Add an API Key, and copy this and paste it as the environment variable RESEND_API
. Assign this environment variable to the apiKey
option for your resendAdapter
, and youâre good to go.
Once you have that in place, you can return to the Resend onboarding page and click "Send email" to move through the onboarding process.
Next, you're going to want to add a domain in Resend. So we'll add our domain and hit add.
And now you're going to need to add these DNS records to your DNS providerâand each DNS provider is going to be different. Once you are done, you can click verify DNS records within Resendâthis should only take a few seconds.
You can now test your Resend integration by restarting your server and following the forgot password flow at your Payload login screen. Upon restarting your server, return to the login page, and click Forgot Passwordâwe should receive an email asking to reset our password!
Once your email adapter is configured (either with Nodemailer or Resend), you can use req.payload.sendEmail()
inside any hook to send emails based on changes in your collections or globals.
Hereâs an example of how to send an email when a document in the posts
collection changes:
And now an email will be sent any time a change is made to this collection.
These email adapters are just another example of how flexible and powerful Payload really is.
You can do more than just send error messages and forgot password requests via email. You can also use this to notify teams or users that changes have been made in different globals or collections, or if you have an important collection, you can send emails whenever an item in a collection has been deleted.