NodeMailer
NodeMailer is a popular Node.js module that allows you to send emails from your Node.js applications. It works by providing an easy interface to communicate with email servers using the Simple Mail Transfer Protocol (SMTP) or other transport mechanisms.
Here's how NodeMailer works:
1. Setup and Configuration
When you use NodeMailer, the first step is to create a transporter object. This transporter is responsible for handling the email sending process. The configuration of the transporter determines how and where your emails are sent.
NodeMailer typically uses SMTP, a protocol for sending emails. SMTP configuration includes details like the email service provider (e.g., Gmail, Yahoo), authentication credentials (username and password), and other settings like security protocols (TLS/SSL).
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
service: "gmail", // or 'Yahoo', 'Outlook', etc.
auth: {
user: "your-email@gmail.com", // Your email
pass: "your-app-password", // Your app password
},
});
2. Email Composition
Once the transporter is set up, you can compose the email. This involves defining the following:
Sender Address (from): The email address from which the email will be sent.
Recipient Address (to): The email address to which the email will be sent.
Subject (subject): The subject line of the email.
Content (text or html): The body of the email. You can send plain text, HTML, or both.
const mailOptions = {
from: "your-email@gmail.com",
to: "recipient-email@example.com",
subject: "Test Email from NodeMailer",
text: "Hello, this is a test email sent using NodeMailer and Gmail.",
html: "<h1>Hello</h1><p>This is a test email sent using <b>NodeMailer</b>.</p>",
};
3. Sending the Email
The sendMail method of the transporter object is used to send the email. This method sends the email to the SMTP server, which then processes it and delivers it to the recipient's email server.
Callback: The sendMail method takes a callback function that handles the response. If the email is sent successfully, you get a success response. If there’s an error (like wrong credentials or server issues), you get an error message.
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log("Error:", error);
}
console.log("Email sent successfully:", info.response);
});
4. Handling Different Transport Mechanisms
While SMTP is the most common, NodeMailer can work with other transport mechanisms, such as:
- Sendmail: A Unix-based command to send emails.
- SES (Amazon Simple Email Service): An email service provided by AWS.
- Direct Transport: Sending emails directly to the recipient's server without using an intermediary SMTP server.
5. Advanced Features
- Attachments: You can send emails with attachments like files, images, or documents.
- Templates: You can use HTML templates for consistent formatting of emails.
- Custom Headers: Adding custom headers for more control over the email content and metadata.
See Full Code :
import nodemailer from "nodemailer";
// connect SMTP server :
const transporter = nodemailer.createTransport({
service: "gmail", // your service Provider
auth: {
user: "sender@gmail.com", // sender's mail
pass: " 12 digit app password if you use Gmail , or other can be very", // sender's mail app passcode
// dont use your mail account password okey...
},
});
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// send mail with defined transport object
const info = await transporter.sendMail({
from: {
name: "NodeMialer", // name you want to display in mail
address: "sender@gmial.com", // sender address
},
to: "shreeraj.on7@gmail.com , bhai2@gmail.com", // list of receivers
subject: "Hello ✔ i mmmmmmm", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body & you can pass files also through "attachment" key ..
});
console.log("Message sent: %s", info.messageId);
// Message sent: <d786aa62-4e0a-070a-47ed-0b0666549519@ethereal.email>
}
main().catch(console.error);
// node index.js okey.....
Check Your Email in Sent Box & we Sent the email , we can use for email verification.
How to create App Password For Your Gmail?
1. Click on Manage Your google Account
2. Enable Two-Step Verification Process
3. open Search , and search "App Password"
4. enter project name & create App password
5. copy and past 12 digit app password { with spaces (after 4 digit have space)}