Introduction to Nodemailer: The Ultimate Guide for Sending Emails with Node.js
Nodemailer is a module for Node.js applications that allows you to easily send emails. It’s a powerful tool for developers to integrate email sending capabilities in their applications. Let’s dive into the world of Nodemailer, understand its API, and explore some practical examples.
Getting Started
First, you need to install Nodemailer in your Node.js application:
npm install nodemailer
Creating a Transporter
To send emails using Nodemailer, you need to create a transporter. The transporter is an object that defines how emails are sent: the service, authentication details, etc.
const nodemailer = require('nodemailer'); let transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: 'your-email@gmail.com', pass: 'your-email-password' } });
Sending an Email
Once you have your transporter set up, you can use it to send emails:
let mailOptions = { from: 'your-email@gmail.com', to: 'recipient-email@gmail.com', subject: 'Hello from Nodemailer', text: 'This is a test email sent using Nodemailer.' }; transporter.sendMail(mailOptions, function(error, info) { if (error) { return console.log(error); } console.log('Email sent: ' + info.response); });
HTML Emails
Nodemailer also allows sending HTML emails:
let mailOptions = { from: 'your-email@gmail.com', to: 'recipient-email@gmail.com', subject: 'Hello from Nodemailer', html: '<p>This is a test email sent using Nodemailer with <b>HTML</b> content.</p>' };
Attachments
You can send attachments with your emails:
let mailOptions = { from: 'your-email@gmail.com', to: 'recipient-email@gmail.com', subject: 'Hello from Nodemailer', text: 'This email contains an attachment.', attachments: [ { filename: 'text1.txt', path: './text1.txt' } ] };
Using Templates
You can also send emails using templates:
const Email = require('email-templates'); const email = new Email({ message: { from: 'your-email@gmail.com' }, // uncomment below to send emails in development/test env: // send: true, transport: { jsonTransport: true } }); email .send({ template: 'mars', message: { to: 'recipient-email@gmail.com' }, locals: { name: 'Elon' } }) .then(console.log) .catch(console.error);
Application Example
Let’s put it all together into a small application:
const nodemailer = require('nodemailer'); const express = require('express'); const app = express(); let transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: 'your-email@gmail.com', pass: 'your-email-password' } }); app.get('/send', (req, res) => { let mailOptions = { from: 'your-email@gmail.com', to: 'recipient-email@gmail.com', subject: 'Hello from Nodemailer', text: 'This is a test email sent using Nodemailer.' }; transporter.sendMail(mailOptions, (error, info) => { if (error) { return res.status(500).send(error.toString()); } res.send('Email sent: ' + info.response); }); }); app.listen(3000, () => { console.log('Server started on port 3000'); });
With this application, you can start your server and hit the /send
endpoint to trigger an email.
Mastering Nodemailer will drastically improve your application’s communication capabilities by integrating reliable email sending features. Happy emailing!
Hash: 67f58c00c7d5bc0382f4eae9d3fccb4e1706a867d3f2cc2f65e36794bc32202b