Documentation
Source Code
lib
mail.ts(Nodemailer)

mail.ts (Nodemailer)

This module exports functions to send different types of emails using Nodemailer. It utilizes a configured SMTP server to send emails.

Use Server Configuration

The module utilizes a configured SMTP server to send emails using the Nodemailer library.

import nodemailer from "nodemailer";
 
const domain = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000";
const senderMail = process.env.NODEMAILER_EMAIL;
const transporter = nodemailer.createTransport({
  pool: true,
  service: "hotmail",
  port: 2525,
  auth: {
    user: senderMail,
    pass: process.env.NODEMAILER_EMAIL_PASSWORD,
  },
  maxConnections: 1,
});

Explanation

The module initializes a Nodemailer transporter using the provided SMTP server configuration. It exports multiple functions to send different types of emails such as verification emails, two-factor authentication (2FA) code emails, and password reset emails.

sendVerificationEmail

Sends a verification email to the specified email address with a confirmation link.

export const sendVerificationEmail = async (
  name: string,
  email: string,
  token: string,
) => {
  const confirmLink = `${domain}/verify-email?token=${token}`;
 
  const mailOptions = {
    from: senderMail,
    to: email,
    subject: "Confirm your email",
    html: `<p>Click <a href="${confirmLink}">here</a> to confirm email.</p>`,
  };
  await new Promise((resolve, reject) => {
    transporter.sendMail(mailOptions, (error: any, info: any) => {
      if (error) {
        console.error(error);
        reject(error);
      } else {
        resolve(info);
      }
    });
  });
};

sendTwoFactorTokenEmail

Sends a two-factor authentication (2FA) code email to the specified email address.

export const sendTwoFactorTokenEmail = async (
  name: string,
  email: string,
  token: string,
) => {
  const mailOptions = {
    from: `${email}`,
    to: email,
    subject: "2FA Code",
    html: `<p>Your 2FA code: ${token}</p>`,
  };
  await new Promise((resolve, reject) => {
    transporter.sendMail(mailOptions, (error: any, info: any) => {
      if (error) {
        console.error(error);
        reject(error);
      } else {
        resolve(info);
      }
    });
  });
};

sendResetEmail

Sends a password reset email to the specified email address with a reset password link.

export const sendResetEmail = async (
  name: string,
  email: string,
  token: string,
) => {
  const resetLink = `${domain}/new-password?token=${token}`;
  const mailOptions = {
    from: senderMail,
    to: email,
    subject: "Reset your password",
    html: `<p>Click <a href="${resetLink}">here</a> to reset password.</p>`,
  };
  await new Promise((resolve, reject) => {
    transporter.sendMail(mailOptions, (error: any, info: any) => {
      if (error) {
        console.error(error);
        reject(error);
      } else {
        resolve(info);
      }
    });
  });
};

Environment Variables

Before using the email sending functions, ensure that the following environment variables are set:

  • NEXT_PUBLIC_BASE_URL: The base URL of the application (optional, defaults to "http://localhost:3000 (opens in a new tab)").
  • NODEMAILER_EMAIL: The sender email address for sending emails.
  • NODEMAILER_EMAIL_PASSWORD: The password for the sender email address.