Introduction to Nodemailer
Nodemailer is a powerful and versatile module for Node.js that allows you to send emails with ease. Whether you need to send simple text emails, HTML emails, attachments, or even integrate with various email services, Nodemailer has got you covered. In this comprehensive guide, we’ll explore the various APIs offered by Nodemailer and provide multiple code snippets to help you get started quickly.
Installing Nodemailer
npm install nodemailer
Basic Example
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
});
let mailOptions = {
from: 'youremail@gmail.com',
to: 'recipientemail@gmail.com',
subject: 'Hello from Nodemailer',
text: 'This is a test email sent using Nodemailer'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Email sent: ' + info.response);
});
Sending HTML Emails
let htmlMailOptions = {
from: 'youremail@gmail.com',
to: 'recipientemail@gmail.com',
subject: 'Hello from Nodemailer',
html: 'Welcome
This is an HTML email sent using Nodemailer.
'
};
transporter.sendMail(htmlMailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('HTML Email sent: ' + info.response);
});
Sending Attachments
let attachmentMailOptions = {
from: 'youremail@gmail.com',
to: 'recipientemail@gmail.com',
subject: 'Email with Attachment',
text: 'Please find the attachment below.',
attachments: [
{
filename: 'file.txt',
path: './file.txt'
}
]
};
transporter.sendMail(attachmentMailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Attachment email sent: ' + info.response);
});
Using a Custom SMTP Server
let customTransporter = nodemailer.createTransport({
host: 'smtp.yourserver.com',
port: 587,
secure: false,
auth: {
user: 'yourusername',
pass: 'yourpassword'
}
});
let customMailOptions = {
from: 'you@yourdomain.com',
to: 'recipientemail@gmail.com',
subject: 'Custom SMTP Server',
text: 'This email is sent using a custom SMTP server.'
};
customTransporter.sendMail(customMailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Custom SMTP email sent: ' + info.response);
});
Using OAuth2 Authentication
let oauth2Transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: 'youremail@gmail.com',
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
refreshToken: 'YOUR_REFRESH_TOKEN',
accessToken: 'YOUR_ACCESS_TOKEN'
}
});
let oauth2MailOptions = {
from: 'youremail@gmail.com',
to: 'recipientemail@gmail.com',
subject: 'OAuth2 Authentication',
text: 'This email is sent using OAuth2 authentication.'
};
oauth2Transporter.sendMail(oauth2MailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('OAuth2 email sent: ' + info.response);
});
Example App with Nodemailer
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/send-email', (req, res) => {
let mailOptions = {
from: req.body.from,
to: req.body.to,
subject: req.body.subject,
text: req.body.text
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return res.status(500).send(error.toString());
}
res.status(200).send('Email sent: ' + info.response);
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
As you can see, Nodemailer is an incredibly flexible and powerful tool for sending emails from your Node.js applications. Whether you need to send simple text emails, HTML emails, attachments, or integrate with various email services, Nodemailer has the functionality you need.
Hash: 67f58c00c7d5bc0382f4eae9d3fccb4e1706a867d3f2cc2f65e36794bc32202b