Introduction to Nodemailer
Nodemailer is a widely-used module for Node.js applications that enables to send emails easily and reliably. Whether you are looking to integrate email functionalities in your web application or setting up an automated email notification system, Nodemailer is the go-to solution. This guide will introduce you to Nodemailer and provide detailed explanations along with code snippets to help you get started.
Getting Started with Nodemailer
const nodemailer = require('nodemailer'); // Create a transporter object using SMTP transport let transporter = nodemailer.createTransport({ host: 'smtp.example.com', port: 587, secure: false, // true for 465, false for other ports auth: { user: 'username@example.com', pass: 'password' } });
Sending an Email
let mailOptions = { from: '"Sender Name"', to: 'receiver@example.com', subject: 'Hello', text: 'Hello world?', html: 'Hello world?' }; transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); });
CC/BCC Functionality
let mailOptions = { from: '"Sender Name"', to: 'receiver@example.com', cc: 'cc@example.com', bcc: 'bcc@example.com', subject: 'Hello with CC and BCC', text: 'Hello world with CC and BCC?', html: 'Hello world with CC and BCC?' }; transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); });
Attachments
let mailOptions = { from: '"Sender Name"', to: 'receiver@example.com', subject: 'Hello with Attachment', text: 'Hello world with attachment?', html: 'Hello world with attachment?', attachments: [ { filename: 'text1.txt', content: 'hello world!' }, { filename: 'text2.txt', path: '/path/to/file' } ] }; transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); });
Sending HTML Emails
let mailOptions = { from: '"Sender Name"', to: 'receiver@example.com', subject: 'Hello with HTML', html: ` Hello
This is an HTML email.
` }; transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message sent: %s', info.messageId); });
Example Application
Here’s a sample Node.js application demonstrating the use of Nodemailer APIs we discussed above:
const nodemailer = require('nodemailer'); async function sendMail() { let transporter = nodemailer.createTransport({ host: 'smtp.example.com', port: 587, secure: false, // true for 465, false for other ports auth: { user: 'username@example.com', pass: 'password' } }); let mailOptions = { from: '"Sender Name"', to: 'receiver@example.com', subject: 'Hello with Attachment', text: 'Hello world with attachment?', html: 'Hello world with attachment?', attachments: [ { filename: 'text1.txt', content: 'hello world!' } ] }; try { let info = await transporter.sendMail(mailOptions); console.log('Message sent: %s', info.messageId); } catch (error) { console.log(error); } } sendMail();
This simple app initializes a Nodemailer transporter, defines email options, and sends the mail using the sendMail() function. You can expand this example further based on your requirements.
Hash: 67f58c00c7d5bc0382f4eae9d3fccb4e1706a867d3f2cc2f65e36794bc32202b