Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
nodemailer
Advanced tools
Nodemailer is a module for Node.js applications to allow easy email sending. It supports various transport methods and has a simple setup process for sending emails.
Send Emails
This feature allows you to send emails using Nodemailer. The code sample shows how to set up a transporter using Gmail, define mail options, and send an email.
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'your.email@example.com',
pass: 'yourpassword'
}
});
let mailOptions = {
from: 'your.email@example.com',
to: 'recipient@example.com',
subject: 'Test Email Subject',
text: 'Hello world?',
html: '<b>Hello world?</b>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
HTML Email Content
Nodemailer allows you to send HTML content in your emails. The code sample demonstrates how to send an email with HTML content.
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
// transport configuration
});
let mailOptions = {
from: 'your.email@example.com',
to: 'recipient@example.com',
subject: 'HTML Email',
html: '<h1>Welcome</h1><p>That was easy!</p>'
};
transporter.sendMail(mailOptions, function(error, info){
// callback
});
Attachments
Nodemailer supports sending attachments in emails. The code sample shows how to attach a file to an email.
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
// transport configuration
});
let mailOptions = {
from: 'your.email@example.com',
to: 'recipient@example.com',
subject: 'Attachment',
text: 'Please find the attachment.',
attachments: [
{
filename: 'file.txt',
path: '/path/to/file.txt'
}
]
};
transporter.sendMail(mailOptions, function(error, info){
// callback
});
Custom Transport Methods
Nodemailer allows the use of custom transport methods for sending emails. The code sample illustrates how to use a custom transport plugin.
const nodemailer = require('nodemailer');
const customTransport = require('my-custom-transport');
let transporter = nodemailer.createTransport(customTransport({
// custom transport options
}));
// send mail with defined transport object
transporter.sendMail({
// mail options
}, function(error, info){
// callback
});
A simple Node.js module for Mailgun. Mailgun-js is designed to work with the Mailgun API for sending, receiving, and tracking emails. It's similar to Nodemailer in functionality but is specifically tailored for Mailgun's service.
The official Node.js client library for interacting with the Postmark API. This package is intended for sending emails through Postmark's transactional email service. Unlike Nodemailer, which is more generic, Postmark is focused on providing a robust service for transactional emails.
Amazon Web Services SDK for JavaScript in Node.js. It includes support for SES (Simple Email Service), which can be used to send emails. While Nodemailer is a standalone package for email, aws-sdk is a comprehensive cloud services SDK that includes email sending capabilities among many other AWS services.
Nodemailer is an easy to use module to send e-mails with Node.JS (using SMTP or sendmail) and is unicode friendly - You can use any characters you like ✔
This version of Nodemailer is built from scratch and might break some existing scripts, so beware while upgrading.
Use DocumentUp to read this README in a more structured way (with TOC).
If you want to parse generated or received e-mail instead of sending it, check out MailParser.
If you only want to generate the raw e-mail stream, check out MailComposer.
If you only want to communicate with the SMTP (both as client and the server), check out simplesmtp.
This is a complete example to send an e-mail with plaintext and HTML body
var nodemailer = require("nodemailer");
var transport = new nodemailer.Transport("SMTP",{
service: "Gmail",
auth: {
user: "gmail.user@gmail.com",
pass: "userpass"
}
});
var mailOptions = {
transport: transport, // transport method to use
from: "Sender Name <sender@example.com>", // sender address
to: "receiver1@example.com, receiver2@example.com", // list of receivers
subject: "Hello!", // Subject line
text: "Hello world!", // plaintext body
html: "<b>Hello world!</b>" // html body
}
nodemailer.sendMail(mailOptions, function(error){
if(error){
console.log(error);
}else{
console.log("Message sent!");
}
transport.close(); // let's shut down the connection pool
});
Install through NPM
npm install nodemailer
Currently v0.3 is not published on NPM so you should install it manually
Use the easy way
npm install https://github.com/andris9/Nodemailer/tarball/v0.3
Or the more complicated way
cd ~/node_modules
git clone git@github.com:andris9/Nodemailer.git nodemailer
cd nodemailer
git fetch origin v0.3:v0.3
git checkout v0.3
npm install
Include the module
var nodemailer = require("nodemailer");
An e-mail can be sent with sendMail(mailOptions, callback)
command
nodemailer.send_mail(mailOptions, callback);
Where
Before you can send any e-mails you need to set up a transport method. This can
be done with new nodemailer.Transport(type, options)
where type
indicates
the transport protocol and options
defines how it used.
var transport = new nodemailer.Transport("SMTP", {smtp_options});
The same transport object can and should be reused several times.
When the transport method is defined, it should be attached to the message
object as transport
var transport = new nodemailer.Transport("SMTP", {smtp_options});
var mailOptions = {
transport: transport,
from: "sender@tr.ee",
to: "receiver@tr.ee"
...
};
Required type
parameter can be one of the following:
SMTP is different from the other transport mechanisms, as in its case a connection pool is created. All the connections try to stay alive as long as possible and are reusable to minimize the protocol overhead delay - for example setting up TLS for authenticating is relatively lengthy process (in CPU terms, not by human terms), you do not want to do it several times.
Possible SMTP options are the following:
service
)service
)false
, not needed with service
){user:"...", pass:"..."}
false
)Example:
var transport = new nodemailer.Transport("SMTP", {
service: "Gmail",
auth: {
user: "gmail.user@gmail.com",
pass: "userpass"
}
});
NB! if you want to close the pool (cancel all open connections) you can use
transport.close()
var transport = new nodemailer.Transport("SMTP",{});
...
transport.close(); // close the pool
SES is actually a HTTP based protocol, the compiled e-mail and related info (signatures and such) are sent as a HTTP request to SES servers.
Possible SES options are the following:
Example:
var transport = new nodemailer.Transport("SES", {
AWSAccessKeyID: "AWSACCESSKEY",
AWSSecretKey: "AWS/Secret/key"
});
Sendmail transport method streams the compiled message to the stdin of sendmail command.
Configuration is really easy, the options parameter is optional but you can use it to define the path to the sendmail command
var transport = new nodemailer.Transport("Sendmail", "/usr/bin/sendmail");
If you want to use a well known service as the SMTP host, you do not need
to enter the hostname or port number, just use the service
parameter.
Currently cupported services are:
Predefined service data covers host
, port
and secure connection settings,
any other parameters (ie. auth
) need to be set separately.
The following are the possible fields of an e-mail message:
sender@server.com
or formatted Sender Name <sender@server.com>
To:
fieldCc:
fieldBcc:
fieldReply-To:
field{"X-Key-Name": "key value"}
(NB! values as passed as is, you should do your own encoding to 7bit if needed)All text fields (e-mail addresses, plaintext body, html body) use UTF-8 as the encoding. Attachments are streamed as binary.
Example:
var transport = new nodemailer.Transport("Sendmail");
var mailOptions = {
transport: transport,
from: "me@tr.ee",
to: "me@tr.ee",
subject: "Hello world!",
text: "Plaintext body"
}
nodemailer.sendMail(mailOptions, function(){});
Attahcment object consists of the following properties:
fileName
propertyOne of contents
or filePath
must be specified, if both are missing, the attachment
will be discarded. Other fields are optional.
Attachments can be added as many as you want.
All the e-mail addresses can be plain e-mail address
username@example.com
or with formatted name (includes unicode support)
"Ноде Майлер" <username@example.com>
To, Cc and Bcc fields accept comma separated list of e-mails. Formatting can be mixed.
username@example.com, "Ноде Майлер" <username@example.com>, "Name, User" <username@example.com>
You can even use unicode domain and user names, these are automatically converted to the required form
"Uncode Domain" <info@müriaad-polüteism.info>
Attachments can be used as embedded images in the HTML body. To use this
feature, you need to set additional property of the attachment - cid
(unique
identifier of the file) which is a reference to the attachment file. The same
cid
value must be used as the image URL in HTML (using cid:
as the URL
protocol, see example below).
NB! the cid value should be as unique as possible!
var html = "Embedded image: <img src='cid:unique@node.ee' />";
var attachments = [{
filename: "image.png",
filePath: "/path/to/file",
cid: "unique@node.ee"
}];
Return callback gets two parameters
Run the tests with npm in Nodemailer's directory
npm test
There aren't currently many tests for Nodemailer but there are a lot of tests in the modules that are used to generate the raw e-mail body and to use the SMTP client connection.
Nodemailer in itself is actually more like a wrapper for my other modules mailcomposer for composing the raw message stream and simplesmtp for delivering it, by providing an unified API. If there's some problems with particular parts of the message composing/sending process you should look at the appropriate module.
Nodemailer is licensed under MIT license. Basically you can do whatever you want to with it.
FAQs
Easy as cake e-mail sending from your Node.js applications
The npm package nodemailer receives a total of 3,096,341 weekly downloads. As such, nodemailer popularity was classified as popular.
We found that nodemailer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.