NextJS send mail using nodemailer using SMTP

import { sendMailAction } from "@/actions/action";

const Page = () => {
  return (
    <form action={sendMailAction}>
      <button type="submit">Send Mail</button>
    </form>
  );
};

export default Page;
"use server";

import { sendMail } from "@/lib/node-mailer";

export const sendMailAction = async () => {
  console.log("acgtion");
  try {
    await sendMail("[email protected]", "Test Subject", "This is a test");
  } catch (error) {
    console.log(error);
  }
};
import nodemailer from "nodemailer";

export const sendMail = async (
  email: string,
  subject: string,
  text: string
) => {
  const transporter = nodemailer.createTransport({
    host: "your.smtp.server",
    port: 587,
    secure: false,
    auth: {
      user: "[email protected]",
      pass: "your_email_password",
    },
    tls: {
      rejectUnauthorized: false,
    },
  });
  try {
    await transporter.sendMail({
      from: "[email protected]",
      to: email,
      subject: subject,
      text: text,
    });
  } catch (error) {
    console.log(error);
  }
};

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *