Email Sending in JavaScript (Node.js) — Libraries and Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Email Sending in JavaScript (Node.js): A Complete Guide



Sending emails is one of the key functions of any modern web application. Whether it's notifications, registration confirmation, newsletters, or password recovery — the ability to programmatically send email is essential for every developer. In this material, we will take a detailed look at how to organize email sending in JavaScript (Node.js) using popular libraries, SMTP settings, and best practices.



Why Send Email from Node.js?

Node.js is an asynchronous JavaScript runtime environment that is excellent for handling a large number of requests. Sending emails via Node.js allows you to:

  • Automate user notifications;
  • Integrate email newsletters with the backend;
  • Handle sending errors in real time;
  • Use one language (JavaScript) for the entire project.


Main Libraries for Sending Email in Node.js

Today, there are several proven libraries. Let's look at the most popular ones:



1. Nodemailer

Nodemailer is the de facto standard for sending email in Node.js. The library supports SMTP, built-in templates, attachments, HTML emails, and OAuth2. It is free and does not require external services.

// Installation: npm install nodemailerconst nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({ host: 'smtp.example.com', port: 587, secure: false, // true for port 465 auth: { user: 'user@example.com', pass: 'your-password' }});

async function sendMail() { const info = await transporter.sendMail({ from: '"Sender" <from@example.com>', to: 'to@example.com', subject: 'Hello from Node.js', text: 'This is a test email.', html: '<b>This is a test email.</b>' }); console.log('Email sent:', info.messageId);}sendMail().catch(console.error);


2. SendGrid (via @sendgrid/mail)

SendGrid is a cloud email delivery service from Twilio. The @sendgrid/mail library allows you to easily send emails via the SendGrid API. It is suitable for large-scale projects.

// Installation: npm install @sendgrid/mailconst sgMail = require('@sendgrid/mail');sgMail.setApiKey('YOUR_SENDGRID_API_KEY');

const msg = { to: 'recipient@example.com', from: 'sender@example.com', subject: 'Sending via SendGrid', text: 'Hello from SendGrid!', html: '<strong>Hello from SendGrid!</strong>'};

sgMail.send(msg) .then(() => console.log('Email sent')) .catch(error => console.error(error));


3. Mailgun.js

Mailgun is another popular service for developers. The mailgun.js library provides a convenient interface for sending emails and managing mailings.

// Installation: npm install mailgun.jsconst formData = require('form-data');const Mailgun = require('mailgun.js');const mailgun = new Mailgun(formData);

const mg = mailgun.client({ username: 'api', key: 'YOUR_MAILGUN_API_KEY'});

mg.messages.create('sandbox.mailgun.org', { from: 'Mailgun Sandbox <postmaster@sandbox.mailgun.org>', to: ['recipient@example.com'], subject: 'Hello from Mailgun', text: 'Test email via Mailgun.'}).then(msg => console.log(msg)).catch(err => console.error(err));


Setting up SMTP for Sending Email

If you are using your own mail server or a Gmail/Yandex account, you will need to configure SMTP. Here is an example for Gmail:

const transporter = nodemailer.createTransport({    service: 'gmail',    auth: {        user: 'your@gmail.com',        pass: 'your-app-password' // Use an app password    }});

Recommendations