Send Emails with Node.js Using Nodemailer and Twilio Sendgrid
A step by step tutorial on how to Send Emails with Node.js Application Using Nodemailer and Twilio Sendgrid.
In this article, we're gonna learn how to send emails from within our node application using 2 third party services, Nodemailer and SendGrid by Twilio.
Now, sending out emails to the users is really common today and it's already become an integral part of pretty much any modern application. Be it, a welcome email that we may want to send to a new user who just signed up on our application or a notification email that we definitely want to send to our existing users informing them about the alerts they might have subscribed for(at some point) in the past. So let's see how we can easily and quickly do all of that.
Nodemailer
This is a module that makes it really easy to send out emails from your NodeJs code. As the name suggests, it can only be used with NodeJs applications because it's a . . . Node - Mailer. (Easy to remember, right?)
Nodemailer is a module for Node.js applications to allow easy as cake email sending. The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default.
- According to the Nodemailer Official Document.
Pre-Requisite
There is just one requirement, that we need to cater to before we can start using this module, which is, you should have NodeJs v6.0.0 or newer. That’s it!
Installation
npm install nodemailer
Twilio Sendgrid
SendGrid is basically an Email Delivery Service. They can deliver your transactional as well as marketing emails through their cloud-based email delivery platform.
FYI, there are several other similar Email Delivery Services available out there. I used SendGrid just because I was able to open a free account with them(as long as you just send fewer than or equal to 100 emails per day). This is not a promotion.
Sign up for your free SendGrid account and verify your account.
Create API Key
The SendGrid API Key is a unique key that helps us to connect with the SendGrid API to use it to send out the emails.
Steps to create an API Key:
Sign In to your SendGrid Account
Go to the Settings Tab and then click on the API Keys section
Click on the Create API Key Button
Give your API Key a Name
Give the relevant access permissions
Click on the Create and View Button
Make sure to copy your API Key and put it somewhere safe before you press the Done Button
Please Note: After your API Key has been created, you can just view it once at the time of the creation as SendGrid doesn’t allow you to see it ever again for security reasons.
Now, if you’re wondering why we’re using these third-party services to send out the emails rather than using a node server to handle this part as well? My answer to you will be in 2 parts, yet quite simple, first of all, we don’t want to reinvent the wheel! Secondly, being developers we should understand the intricacies behind using node or express servers to create our own mailing service, it will be a service that can receive and send out hundreds of thousands of emails and should also do that very securely while handling other complexities as well, therefore, it's much easier and faster to use other third-party modules that do our dirty work for us so that we can focus on what’s more important to us; the business logic.
Okay, now let’s see how does our code look like:
const nodemailer = require('nodemailer');
const sendgridTransport = require('nodemailer-sendgrid-transport');
const transport = nodemailer.createTransport(sendgridTransport({
auth: {
api_key: 'paste-your-api-key-here'
}
}))
transport.sendMail({
to: 'theimmigrantprogrammers@gmail.com',
from: 'yourverifiedsendgridemail@gmail.com',
subject: 'Test Email',
html: '<h2>Please Like Share Comment And Subscribe</h2>'
})
.then(console.log('Success!'))
.catch(err => console.log(err))
A Comment on Large Scale Applications:
Sending emails as part of the business logic code is a really heavy task, therefore, it is a good practice to keep a dedicated microservice to handle this part. Also, the email sending should be handled synchronously, which means, that we shouldn’t wait on the email service to finish executing the email send action before moving on to the next task. Essentially, you don’t need to handle the .then( ) block(you saw in the code above) in large scale applications other than some special circumstances.
All right, so that's a wrap on this article. I hope I could do justice with your time today! Hope to see you in one of my next, until then Happy Learning!:)
Helpful Resources: