How to send emails in Sails & Bedrock

If you have used Bedrock, you have probably set up user authentication for your web application. If you haven’t, check out how to do that in this post.

However, you may have seen that the Reset Password flow is broken. What’s up with that, you wonder?

Well, first let’s take a look at what the Reset Password flow looks like:

The problem is in the 2nd step (highlighted yellow). Bedrock will be unable to send emails to users unless you need to tell Bedrock how to send emails.

How do emails work in Bedrock

By default, Bedrock uses Nodemailer, a popular npm package for sending emails. I have been using Nodemailer for years and it has never let me down. Bedrock just puts a small wrapper on top of it.

This small wrapper is called Mailer, and it is a service inside Bedrock. Let’s look at where the code lives.

config/email.js

This is the email configuration file. It stores your email credentials for specific email transports. By default, Bedrock ships with Nodemailer’s Gmail transport. If you want to use Gmail to send emails, you can enter in your Gmail address and an app-specific password here.

api/services/Mailer.js

Bedrock also exposes an email service called Mailer. From anywhere on the server, you can call Mailer.sendMail(options, callback), and an email will be sent using the email configuration that is defined in the Mailer object. You can add additional methods to the Mailer service.

As an example, let’s assume we want to send emails with Gmail.

Sending emails with Gmail

  1. First, let’s generate an Gmail app-specific password.
  2. Then, enter the app-specific password, along with the Gmail address that you want to send emails from inside config/email.js
  3. Restart your server. Your application will now be able to send emails using your Gmail address.
  4. If you want to update what the Reset Password email says, you can alter the text in api/controllers/AuthController#forgotPasswordSubmit.

Integrating with other well-known Email Providers

Ok, so that’s pretty easy, but how do you deal with other email providers?

Well, since Bedrock just uses Nodemailer under the hood, you can use any Nodemailer Transport. For example, let’s say you wanted to use Mailgun:

First, edit api/services/Mailer.js, and update the sendMail method:

sendMail: function (options, callback) {
  var transporter = nodemailer.createTransport({
     service: 'mailgun', //we changed this
     auth: { ... } //your mailgun authentication goes here
  });

  transporter.sendMail({
     from: options.from || emailConfig.mailgun.fromAddress, //pull from config/email.js if needed
     to: options.to,
     subject: options.subject,
     text: options.text
  }, callback);
}

Next, update config/email.js with your mailgun authentication details. That’s all!

Nodemailer supports tons of transports. View the full list of supported transports.

SMTP Emails & Configuring Email Delivery

For other use cases such as SMTP emails and production email configuration, I strongly encourage you guys to read the Nodemailer documentation, as it has a lot of details on different email configurations. If you guys get stuck, feel free to contact me and I will do my best to help you out

 

Up Next:

How to make database changes when your app is in production

How to make database changes when your app is in production