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.
You can use two ways to send an e-mail message: the EmailMessage constructor or the shortcut function send_mail(). The send_mail() function takes all the fields of the e-mail message as a function parameter and sends the e-mail immediately. EmailMessage allows to compose the message object first and send it later with its method send().
Nodemailer is Unicode friendly ✔. You can use any characters you like.
Install through NPM
npm install nodemailer
or download ZIP archive.
If you use NPM then the module is available as var nodemailer = require('nodemailer');
but if you're using the source then var nodemailer = require('./path_to_nodemailer/lib/mail');
Using send_mail()
var nodemailer = require("nodemailer");
nodemailer.send_mail({sender: "me@example.com",
to:"you@example.com",
subject:"Hello!",
body:"Hi, how are you doing?"},
function(error, success){
console.log("Message "+(success?"sent":"failed"));
});
Using EmailMessage
var nodemailer = require("nodemailer");
var mail = nodemailer.EmailMessage({
sender: "me@example.com",
to:"you@example.com"
});
mail.subject = "Hello!";
mail.body = "Hi, how are you doing?";
mail.send(function(error, success){
console.log("Message "+(success?"sent":"failed"));
});
The callback function gets two parameters - error and success. If there's an error, then sending failed and you should check where's the problem. If there's no error value but success is not true then the server wasn't able to process the message correctly. Probably there was timeout while processing the message etc - in this case you should re-schedule sending this e-mail. If success is true then the message was sent successfully.
Before sending e-mails you need to set up SMTP server parameters.
nodemailer.SMTP = {
host: "smtp.example.com",
port: 25,
hostname: "myhost.com",
use_authentication: false,
user: "",
pass: ""
}
See examples/example.js for a complete example.
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:
fieldfilename
and contents
. Property contents
can either be a String or a Buffer (for binary data). filename
is the name of the attachment.There's an optional extra field headers which holds custom header values in the form of {key: value}
. These values will not overwrite any existing header but will be appended to the list.
mail_data = {
sender:"me@example.com",
....
headers: {
'X-My-Custom-Header-Value': 'Visit www.example.com for more 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 cid_value = Date.now()+".image.jpg";
var html = 'Embedded image: <img src="cid:'+cid_value+'" />';
var attachments = [{
filename: "image.png",
contents: IMAGE_CONTENTS,
cid: cid_value
}];
Node.JS v0.3.x doesn't support changing to a secure channel in the middle of a connection (STARTTLS). So when a server requires authentication and this must be done over TLS it's a problem.
Currently the only allowed charset is UTF-8.
Do not use large attachments as the attachment contents are read into memory and the final message body is combined into a single string.
Nodemailer is licensed under MIT compatible 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.