Install

npm install nodemailer

Usage

Create a new app password for your gmail account here.

in .env file add:

# Gmail SMTP
GMAIL_EMAIL_ADDRESS=<replace-with-your-email-address>
GMAIL_APP_PASSWORD=<replace-with-your-app-password>

Example usage with Next.js API route

create a new file app/api/mail-me/route.ts:

import { createTransport } from "nodemailer";
export async function POST(req: Request) {
  const transporter = createTransport({
    service: "gmail",
    auth: {
      user: process.env.GMAIL_EMAIL_ADDRESS,
      pass: process.env.GMAIL_APP_PASSWORD,
    },
  });

  const mailOptions = {
    from: process.env.GMAIL_EMAIL_ADDRESS,
    to: process.env.GMAIL_EMAIL_ADDRESS,
    subject: "Say hello to the ๐Ÿ†",
    html: `<p>Hi ๐Ÿ†, How are you?</p>`,
  };
  await transporter.sendMail(mailOptions);

  return Response.json({ message: "Email sent" });
}