How to Send Emails with Node.JS

AIn this post, Diana Lepilkina will have a look at options of sending emails without using Nodemailer in Node.js.

For more details, read the full guide Sending Emails with 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.
  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.

Even though there are alternatives, Nodemailer is the most popular package, which offers functionality for both email creation and email sending. It’s not limited to one sending method. But it won’t be easy to create a special email template. This is why it might be a good idea to use Nodemailer in combination with another package.

To find all related packages and plugins, search for nodemailer in npm.

Sending HTML emails with dynamic content

In most cases, for transactional emails like registration confirmation or resetting password, you need to use dynamic content. It will be easier and more efficient to do it with one of the template modules.

Let’s experiment with the email-templates package. It has several interesting features:

  • Support for different template engines (Pug is a default one)
  • Email preview (by default) in the development environment
  • Direct email sending. So, you don’t need extra packages like Nodemailer for email sending.

First of all, let’s create our templates, for a frequently occurring scenario: new user registration. In this example, we are working with the default option (for more details and samples of using Pug, refer to Github.)

Code snippets, examples of messages, and tips on testing emails with dynamic content before sending them using Node.js are available in the full guide mentioned at the beginning of this post.

Photo by Allie on Unsplash

#node

How to Send Emails with Node.JS
6.65 GEEK