Codementor Events

Sending Emails with Node.js

Published Apr 01, 2020
Sending Emails with Node.js

Sending emails from Node.js is easy. We have gone over it in our previous blog post on sending emails with Nodemailer. Last time we reviewed Nodemailer’s capabilities we focused on sending HTML emails via SMTP. In this post, we will examine how to send emails with Node.js using popular email servers like Gmail. Also, we will have a look at other transport options and packages to build and send emails from Node.js.  

Building and sending emails with Node.js without Nodemailer

In some guides and tutorials, you might find a note that there are a variety of Node.js email packages but Nodemailer is the best one. It’s not true. In fact, you can barely find a decent alternative to Nodemailer (and I can hardly imagine why you might need it.)

On Github, you can find several Node.js packages related to emails but they won’t offer you a wide functionality. With Nodemailer, you can create HTML emails with attachments and send them via SMTP, SES (wrapper for sending emails via AWS SES), or sendmail

  1. The most similar package is Emaijs. Its features include: 
  • sending emails via SMTP servers (both SSL and TLS) with authentication
  • HTML support and MIME attachments (also, attachments can be added as strings, streams, or file paths)
  • asynchronous sending of queued emails 
  • UTF-8 encoding in headers and body.

So, the main difference is that in Emailjs you will use MIME type to work with attachments, while in Nodemailer you use strings. 

  1. Another quite popular package is email-templates. As you can see from the name, this package is designed for creating various custom templates for Node.js. It features support for automatic inline CSS, stylesheets, embedded images, and fonts. Also, it has an email preview option. The email templates package was made by the creator of the Lad framework. So it’s recommended to use it with Lad. 

  2. One more package worth mentioning here is Mailgen. It is aimed at creating HTML templates for transactional emails. There is a note on Github, that with Mailgen you can “Programmatically create beautiful e-mails using plain old JavaScript.” The package includes several open-source themes as well as supports custom elements like tables, action buttons, etc. It is your choice how to send an email created with Mailgen, but they recommend checking out Nodemailer for this purpose...

Continue reading the full guide on sending emails with Node.js originally on Mailtrap blog.

Discover and read more posts from Andriy Zapisotskyi
get started
post commentsBe the first to share your opinion
Show more replies