How to Send Email Using Nodemailer in Node.js | Node.js Email Tutorial



**How to Send Email Using Nodemailer in Node.js | Email Tutorial using Node.js** https://github.com/harshita795/email_send_demo In this tutorial, we’ll go through the steps to **send emails using Nodemailer in Node.js**, providing a complete guide to setting up and sending custom emails directly from your Node.js application. Nodemailer is a popular email-sending library for Node.js that supports both plain text and HTML emails, as well as attachments, making it perfect for setting up notifications, confirmations, and alerts. ### What You’ll Learn in This Tutorial: 1. **Setting Up Nodemailer in Node.js**
2. **Configuring SMTP Settings** (Gmail, Yahoo, or Custom SMTP Server)
3. **Sending Basic Text and HTML Emails**
4. **Adding Attachments** ### Requirements: – **Node.js** installed on your system
– A **SMTP email service** (e.g., Gmail, Yahoo, or a custom SMTP provider)
– **Nodemailer** library (we’ll install this via npm) ### Step 1: Install Nodemailer To get started, make sure you have Node.js installed. Then, open your terminal in the project folder and install Nodemailer: “`bash
npm install nodemailer
“` ### Step 2: Import Nodemailer and Configure the SMTP Settings Next, create a new JavaScript file (e.g., `sendEmail.js`) and import Nodemailer. You’ll then need to configure the SMTP settings with your email service credentials. For Gmail SMTP configuration, it’s essential to **enable “Less secure app access”** in your Gmail account settings. Here’s an example setup: “`javascript
const nodemailer = require('nodemailer'); const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: 'your-email@gmail.com', // Replace with your email pass: 'your-email-password' // Replace with your email password }
});
“` Alternatively, you can use environment variables for security and store credentials in a `.env` file. ### Step 3: Create the Email Options Now, specify the details of the email, including sender, receiver, subject, and message content. You can customize the email content using plain text or HTML. ### Step 4: Send the Email Use the `.sendMail()` method to send the email. Add a callback function to handle any errors and confirm successful delivery. ### Step 5: Run Your Script Save your file and run it in the terminal: “`bash
node sendEmail.js
“` If everything is configured correctly, you should see a message indicating the email was sent successfully. Check your inbox to confirm the receipt. ### Adding Attachments (Optional): To include attachments, add an `attachments` property to the `mailOptions` object. You can add multiple attachments, specifying file paths, URLs, or base64 data. “`javascript
const mailOptions = { from: 'your-email@gmail.com', to: 'recipient-email@gmail.com', subject: 'Email with Attachment', text: 'Please find the attachment below.', attachments: [ { filename: 'file.txt', path: './path/to/file.txt' } ]
};
“` ### Common Issues and Troubleshooting – **SMTP Authentication Error**: Double-check your credentials. If using Gmail, ensure “Less secure app access” is enabled.
– **Rate Limits**: Free SMTP services like Gmail have limitations. For high-volume emails, consider using a service like SendGrid or Amazon SES.
– **Timeouts or Server Errors**: Check internet connectivity and server response. This guide gives you everything you need to get started with sending emails in Node.js using Nodemailer. It’s an excellent tool for integrating email functionality into your applications, from transactional emails to alerts and notifications. ### Resources:
– [Nodemailer Documentation](https://nodemailer.com/about/)
– [Node.js Official Documentation](https://nodejs.org/en/docs/) #Nodejs #Nodemailer #SendEmail #NodejsEmail #SMTP #JavaScript #CodingTutorial #EmailIntegration #TechTutorial