Sending Email Using Nodemailer

Sending Email Using Nodemailer in Express.js (Full Guide)

Nodemailer হল Node.js অ্যাপ্লিকেশন থেকে email পাঠানোর জন্য সবচেয়ে জনপ্রিয় এবং সহজ একটি প্যাকেজ। User verification, OTP system, password reset, contact form — এসব জায়গায় Email send করা খুবই প্রয়োজন। এই টিউটোরিয়ালে আপনি শিখবেন Nodemailer installation, SMTP setup, Gmail configuration এবং Express.js দিয়ে email পাঠানোর সম্পূর্ণ প্রক্রিয়া।


Nodemailer কী?

Nodemailer একটি Node.js module, যা দিয়ে সহজেই email পাঠানো যায়। এটি SMTP ব্যবহার করে Gmail, Outlook, Yahoo বা যেকোনো private SMTP server দিয়ে Email send করতে পারে।

যেখানে Nodemailer ব্যবহার হয়:
  • User Registration OTP
  • Password Reset Email
  • Contact Form Notifications
  • Order Confirmation
  • System Alerts & Notifications

Nodemailer Install করা

npm install nodemailer

Basic Email Sending Setup (Gmail SMTP)

আমরা Gmail SMTP ব্যবহার করে Email পাঠাবো। Gmail-এর জন্য আপনাকে Google অ্যাকাউন্টে “App Password” তৈরি করতে হবে।

⚠️ গুরুত্বপূর্ণ: Normal Gmail password কাজ করবে না। আপনাকে App Password তৈরি করতে হবে।

Express.js দিয়ে Email Send Example

const express = require("express");
const nodemailer = require("nodemailer");
require("dotenv").config();

const app = express();
app.use(express.json());

app.post("/send-email", async (req, res) => {
    const { to, subject, message } = req.body;

    try {
        // SMTP Transporter Setup
        const transporter = nodemailer.createTransport({
            service: "gmail",
            auth: {
                user: process.env.EMAIL_USER,
                pass: process.env.EMAIL_PASS,
            },
        });

        // Email Content
        const mailOptions = {
            from: process.env.EMAIL_USER,
            to,
            subject,
            html: `

${message}

` }; // Send Email await transporter.sendMail(mailOptions); return res.json({ success: true, message: "Email Sent Successfully!" }); } catch (error) { return res.status(500).json({ success: false, error: error.message }); } }); app.listen(5000, () => console.log("Server running on port 5000"));

.env File Setup

EMAIL_USER=yourgmail@gmail.com
EMAIL_PASS=your-app-password

Gmail App Password কিভাবে পাবেন?

  1. Google Account → Manage Account এ যান
  2. Security ট্যাব এ যান
  3. 2-Step Verification ON করুন
  4. “App Passwords” অপশন সিলেক্ট করুন
  5. App তৈরি করে 16-digit password কপি করুন
  6. .env ফাইলে সেট করুন

HTML Email পাঠানো (Beautiful Email)

html: `
    <div style="padding:20px; background:#f0fdfd;">
        <h2 style="color: teal;">Welcome User!</h2>
        <p>Thank you for joining our platform.</p>
    </div>
`

Attachment সহ Email পাঠানো

attachments: [
    {
        filename: "demo.pdf",
        path: "./files/demo.pdf"
    }
]

Best Practices

  • .env ফাইলে SMTP credentials রাখুন
  • App Password ছাড়া Gmail SMTP ব্যবহার করবেন না
  • Asynchronous email sending-এর জন্য Queue (Bull.js) ব্যবহার করতে পারেন
  • HTML email template মিনিমাল রাখুন

Conclusion

Nodemailer দিয়ে Express.js থেকে Email পাঠানো খুবই সহজ। Gmail, Company SMTP বা Third-Party SMTP—সব কিছুর সঙ্গে এটি কাজ করে। OTP based login, password reset, contact form, order notification — সব জায়গায় Nodemailer অপরিহার্য।

👼 Quiz
/

লোড হচ্ছে...

Interview Questions:

1. Nodemailer কী?

Nodemailer Node.js দিয়ে ইমেইল পাঠানোর জন্য ব্যবহৃত একটি জনপ্রিয় প্যাকেজ।

2. Nodemailer ব্যবহার করে কী কী পাঠানো যায়?

Text email, HTML email এবং attachment সহ ইমেইল পাঠানো যায়।