Nodemailer-otp is a simple Node.js package that allows developers to easily generate OTPs (One Time Passwords) and send email notifications using Nodemailer. This package provides easy integration into your Node.js projects and supports customizable OTP lengths.
- Generate OTPs for use in your application
- Send OTPs and custom messages via email using Nodemailer
- Simple and customizable integration with your Node.js project
- Easy setup and usage
To get started with nodemon-helper
, follow these steps:
npm install nodemailer-otp
Create a .env
file in your project's root directory:
EMAIL_USER=your-email@example.com
EMAIL_PASS=your-email-access-key
-
Gmail Setup:
- Enable "Less Secure Apps" in your Gmail account settings, or
- Generate an App-Specific Password if using 2-factor authentication
- Use the app password in your
.env
file
-
Dependencies:
npm install nodemailer
const NodemailerHelper = require('nodemailer-otp');
require('dotenv').config();
// Initialize the helper
const helper = new NodemailerHelper(process.env.EMAIL_USER, process.env.EMAIL_PASS);
import NodemailerHelper from 'nodemailer-otp';
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();
// Initialize the helper
const helper = new NodemailerHelper(process.env.EMAIL_USER, process.env.EMAIL_PASS);
// Generate a 6-digit OTP
const otp = helper.generateOtp(6);
console.log(`Generated OTP: ${otp}`);
helper.sendEmail('recipient-email@example.com','subject','your message here!', otp)
.then((response ) => {
console.log(response );
})
.catch((err) => {
console.error(err);
});
Constructor for initializing the helper with your email credentials.
-
email
: Your email address -
emailAccessKey
: Email provider access key or app-specific password
Generates an OTP.
-
length
: Number (4) //you can generate any length of otp. - Returns: String containing the generated OTP
Sends OTP via email.
-
recipientEmail
: Recipient's email address -
otp
: OTP to send - Returns: Promise
- Verify "Less Secure Apps" is enabled or use App-Specific Password
- Confirm email credentials in
.env
file are correct
- Check console logs for generated OTP is working
- Review error logs
- Verify email configuration in
.env
- Check email service status and account restrictions
We welcome contributions! Feel free to:
- Fork the repository
- Create a feature branch
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.