Warning
You're browsing the documentation for an old version of Webiny. Consider upgrading your project to Webiny 5.39.x.
What You’ll Learn
  • How to use api-mailer to send e-mails
  • How to use api-mailer with basic configuration
  • How to use api-mailer with advanced configuration

About
anchor

Starting from 5.30.0 version of Webiny we have a mailer package (@webiny/api-mailer) via which you can send e-mails.

Available Mailers
anchor

We have a couple of built-in e-mail mailers:

  • Dummy Mailer - does not send e-mails, just records them in an array which you can access later
  • SMTP Mailer - sends e-mails with the help of nodemailerexternal link library

Dummy Mailer
anchor

A mailer which pretends to be sending e-mails. Used for testing and as a backup in case no other mailer is configured or defined.

You can manually set it as the mailer so e-mails never actually get sent:

SMTP Mailer
anchor

A mailer which sends e-mails via the defined SMTP credentials. In the background it uses nodemailerexternal link library to send the e-mails.

Basic Configuration
anchor

The configuration is done via the environment variables:

If any of these variables is not defined, mailer will default to the Dummy Mailer.

Advanced Configuration
anchor

The advanced configuration is done by creating your own SMTP mailer with custom config. You can use our createSmtpMailer method, you just need to pass the custom configuration.

Here is an example on how to configure custom mailer:

Note that setMailer method supports both setting the variable of Mailer type or an async Mailer factory.

Here is an example of a context plugin which sets mailer via factory:

Usage
anchor

For example, let’s say you want to send an e-mail when user creates an entry in the Person model. You would want to subscribe to the onAfterEntryCreate event and send an e-mail only when added entry to that certain model.