Photo by Clark Tibbs on Unsplash
How to send an Email in NodeJS, and how you can receive and read mail with IMAP instead of sending.
Table of contents
- What is NodeMailer Module?
- Why NodeMailer?
- How can I Install NodeMailer?
- How to send an Email with NodeMailer from your application
- How can I email Multiple Receivers
- How can you send an HTML formatted text in your email?
- Did you want to receive and read mail with IMAP instead of sending?
- Here are some NodeMailer features
- What are the requirements to be able to use NodeMailer in your Node.js application
What is NodeMailer Module?
Nodemailer is a module for Node.js applications that makes it easy to send emails from your computer.
Why NodeMailer?
The project got started back in 2010 when there was no sane option to send email messages, today it is the solution most Node.js users turn to by default.
How can I Install NodeMailer?
You can install NodeMailer in your Node.js application using:
npm install nodemailer
You can now include the module in your application like this:
const nodemailer = require('nodemailer');
How to send an Email with NodeMailer from your application
You can send emails from your server, by using the username and password from your selected email provider to send an email. The example below explained how to use your Gmail account to send an email.
Code Sample:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
});
var mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
Now your server is able to send emails.
How can I email Multiple Receivers
To email more than one receiver, add them to the "to" property of the mailOptions object, separated by commas.
Code Sample:
const mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com, myotherfriend@yahoo.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
}
How can you send an HTML formatted text in your email?
To send HTML formatted text in your email, use the "html" property instead of the "text" property.
Code Sample:
const mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
html: '<h1>Welcome</h1><p>That was easy!</p>'
}
Did you want to receive and read mail with IMAP instead of sending?
You can check out EmailEngine β self-hosted service that allows to make REST request against IMAP and SMTP servers. EmailEngine also sends webhooks whenever something changes on the registered accounts.
Here are some NodeMailer features
A single module with zero dependencies β code is easily auditable, as there are no dark corners.
Heavy focus on security.
Unicode support to use any characters, including emoji πͺ
Windows support β you can install it with npm on Windows just like any other module, there are no compiled dependencies. Use it hassle free from Azure or from your Windows box.
Use HTML content, as well as plain text alternative.
Add Attachments to messages.
Embedded image attachments for HTML content β your design does not get blocked.
Secure email delivery using TLS/STARTTLS
Different transport methods in addition to the built-in SMTP support
Sign messages with DKIM
Custom Plugin support for manipulating messages
Sane OAuth2 authentication
Proxies for SMTP connections
ES6 code β no more unintentional memory leaks, due to hoisted varβs
Autogenerated email test accounts from Ethereal.email
What are the requirements to be able to use NodeMailer in your Node.js application
If you are able to run Node.js version 6 or newer, then you can use Nodemailer.
There are no platform or resource specific requirements. All public Nodemailer methods support both callbacks and Promises (if callback is omitted). You need to have at least Node v8.0.0 if you want to use async..await
with Nodemailer.
Thanks for reading...
Happy Coding!