What is Nodemailer and why is it needed?
\n\nNodemailer is a module for Node.js that allows you to send emails from your JavaScript application. It is one of the most popular and stable libraries in the Node.js ecosystem for working with email. It provides a simple yet powerful API for creating and sending messages via various transports (SMTP, Sendmail, Amazon SES, and others).
\n\nWhy is Nodemailer needed?
\n- \n
- Notification automation: Sending emails about registration, password reset, order confirmation, and other events. \n
- Mass mailings: Creating newsletters, marketing campaigns (with caution to avoid spam). \n
- Feedback: Sending messages from contact forms on the website. \n
- Monitoring and logging: Notifying the administrator about critical errors in the application. \n
- Flexibility: Support for HTML templates, attachments, embedded images, and various encodings. \n
Nodemailer abstracts the complexity of working with email protocols (SMTP, POP3, IMAP) and provides a unified, clear interface.
\n\nInstalling Nodemailer
\n\nInstalling Nodemailer is done via the npm (Node Package Manager) package manager. Make sure you have Node.js installed (version 12 or higher).
\n\nOpen a terminal in the root folder of your project and run the command:
\n\nnpm install nodemailer\n\nAfter installation is complete, you can import the library into your code:
\n\nconst nodemailer = require('nodemailer');\n\nFor projects using ES modules (with "type": "module" in package.json), use the import:
\n\nimport nodemailer from 'nodemailer';\n\nQuick start: sending your first email
\n\nThe simplest way to send an email is to use an SMTP server. In this example, we will use the Gmail service. Important: To work with Gmail, you will need to create an "App Password" or use OAuth2. A regular account password will not work due to Google's security settings.
\n\nconst nodemailer = require('nodemailer');\n\n// 1. Create a transport (sending method)\nconst transporter = nodemailer.createTransport({\n service: 'gmail',\n auth: {\n user: 'your-email@gmail.com', // Your email\n pass: 'your-app-password' // App password\n }\n});\n\n// 2. Configure the email\nconst mailOptions = {\n from: 'your-email@gmail.com',\n to: 'recipient@example.com',\n subject: 'Hello from Nodemailer!',\n text: 'This is a test email sent using Nodemailer.'\n};\n\n// 3. Send the email\ntransporter.sendMail(mailOptions, (error, info) => {\n if (error) {\n return console.log('Error:', error);\n }\n console.log('Email sent:', info.messageId);\n console.log('Preview URL:', nodemailer.getTestMessageUrl(info));\n});\n\n\nThis code creates a transport for Gmail, forms a simple text email, and sends it. On success, you will see the message ID in the console.
\n\nMain methods and functions of Nodemailer
\n\nLet's take a detailed look at the key methods provided by the library.
\n\n1. nodemailer.createTransport(options)
\nWhat it does: Creates a transport object — this is the main configuration that determines exactly how the email will be sent (through which server and with what credentials).
\nParameters:
\n- \n
options(Object): Configuration object. Main properties:\n- \n
host(String): SMTP server address (e.g., 'smtp.gmail.com'). \n Also in library
HTTP Client for Kotlin: Top Libraries for Network Operations
Java QR Code Generator: ZXing (Zebra Crossing) Library
Reactive Programming in JavaScript (Node.js): RxJS & Observables
OAuth Client for Java: ScribeJava and Spring Security OAuth2
Chai.js for JavaScript (Node.js): Complete Guide with Examples
Recommendations