With Payload, you don't have to abandon what you're already familiar with. Stick to your trusty SMTP if you like to keep things simple. Or, if you're a fan of Gmail or Outlook, go ahead and integrate them with ease. You can even bring in other powerful email service tools like SendGrid, Resend, HubSpot and more.
Integrating email with Payload is free, flexible and highly extensible. No matter what kind of emails you need to send – from newsletters, transactional and marketing emails, to those crucial authentication emails – we've got you covered.
In this post, we’ll walk through the process of configuring email with Payload and cover everything you need to get up and running. Let’s dive in!
Payload utilizes Nodemailer to produce a versatile email transporter which can then be used anywhere in your application.
For those who are new to Nodemailer, it is a powerful module in the Node.js ecosystem that greatly simplifies the process of sending email. We recommend taking a look at the Nodemailer docs if you want to learn more.
If you have used Nodemailer before, this process will be familiar. Simply create a new transport and pass it to the email
property in your payload.init()
function.
Once you add your email configuration, you can send emails from anywhere in your application simply by calling Payload.sendEmail({})
. Neat, huh?
The email
property takes the following options:
nodemailer.createTransport({})
There are two ways to create a Nodemailer-compatible transport:
If you are on version 1.7.0 or later, you can add your email options directly in to your payload.config.
For versions < 1.7.0
you will need to pass your email options to payload.init()
which is usually found in the server.ts
file.
After adding your email options, your payload.init() or payload.config
should look something like this:
If you do not provide a transport or transportOptions, Payload will initialize an ethereal capture service. Ethereal is a free email caching service which captures all outbound emails. Using this service can be really useful for testing emails when you’re working in a development environment.
To use this service, logMockCredentials
must be set to true
. This will output the ethereal credentials to your console after startup, you will then use these to login to ethereal.email and view any emails that are sent during development.
Pass any valid Nodemailer options to transportOptions
and Payload will create the transporter for you.
You can use transportOptions to configure:
1. SMTP
2. An email service
Nodemailer will automatically provide the connection details (host, port, etc) for several well known email services. For example if you want to use Gmail, you simply need to provide the service name like this:
3. An external transport, a nodemailer plugin or similar
Nodemailer has created packages that integrate popular email vendors for you, such as SendGrid:
This option allows you to manually create a transport, this supports SMTP and email services.
You can make use of nodeMailer.createTransport({})
for support of well known email services and browse this list of options that you can define.
More examples of using nodeMailer.createTransport({}) can be found in the Nodemailer documentation.
Once you have configured your transporter, you can start sending emails from anywhere inside your Payload project by calling payload.sendEmail({})
.
payload.sendEmail({})
takes properties of to
, from
, subject
, and html
.
There are many ways to include data directly from your project into your emails. Whether it is using hooks, making API requests, fetching data from globals or anything else you can think of.
For example, sending order details when there is a new submission to the Orders collection:
Payload’s collection and field hooks allow you to define specific conditions which will trigger an email to be sent.
Like sending an email every time you receive a newsletter signup:
Or sending a welcome email to new users:
Payload makes auth-enabled collections super simple to integrate with email by handling forgotPassword
and verify
for you.
Each auth-enabled collection has forgotPassword
and verify
options that you can pass generateEmailSubject
and generateEmailHTML
functions to. The function accepts one argument containing { req, token, user }
.
Payload doesn't ship a default HTML templating engine, so you are free to add whatever suits you best. Make your email templates highly dynamic by using Handlebars, a templating language that combines HTML, plain text and expressions. The expressions are included in the html template surrounded by double curly braces.
Here is a simple but powerful example that ties everything together in one function:
The template.html
file that is being used in sendEmailWithTemplate
can be any HTML file of your choice. You can find this template in our email example, feel free to use this as a starter template and add your own custom CSS.
We have an example repo where you can see these code snippets being used and try them out in real time.
Payload provides a super flexible solution for integrating email functionality, in production and development. Using NodeMailer allows email sending to be much simpler and cleaner. A basic setup can be configured with just a few lines of code, and you can absolutely extend it to accommodate more advanced features.
With this post and the example repo you will have tools you need to start working with email in Payload - as always, feel free to reach out if you have any questions or feedback.
We're trying to change the CMS status quo by delivering editors with a great experience, but first and foremost, giving developers a CMS that they don't absolutely despise working with. All of our new features are meant to be extensible and work simply and sanely.