Skip to content

Instantly share code, notes, and snippets.

@heytulsiprasad
Created December 21, 2024 07:43
Show Gist options
  • Save heytulsiprasad/3270bf3f733ee7f4f912b573a218c977 to your computer and use it in GitHub Desktop.
Save heytulsiprasad/3270bf3f733ee7f4f912b573a218c977 to your computer and use it in GitHub Desktop.
Mail sender template nodemailer
"use server";
import nodemailer from "nodemailer";
const SMTP_SERVER_HOST = process.env.SMTP_SERVER_HOST;
const SMTP_SERVER_USERNAME = process.env.SMTP_SERVER_USERNAME;
const SMTP_SERVER_PASSWORD = process.env.SMTP_SERVER_PASSWORD;
const SITE_MAIL_RECIEVER = process.env.SITE_MAIL_RECIEVER;
const transporter = nodemailer.createTransport({
service: "gmail",
host: SMTP_SERVER_HOST,
port: 587,
secure: true,
auth: {
user: SMTP_SERVER_USERNAME,
pass: SMTP_SERVER_PASSWORD,
},
});
export async function sendMail({
email,
sendTo,
subject,
text,
html,
}: {
email: string;
sendTo?: string;
subject: string;
text: string;
html?: string;
}) {
try {
const isVerified = await transporter.verify();
} catch (error) {
console.error(
"Something Went Wrong",
SMTP_SERVER_USERNAME,
SMTP_SERVER_PASSWORD,
error,
);
return;
}
const info = await transporter.sendMail({
from: email,
to: sendTo || SITE_MAIL_RECIEVER,
subject: subject,
text: text,
html: html ? html : "",
});
console.log("Message Sent", info.messageId);
console.log("Mail sent to", SITE_MAIL_RECIEVER);
return info;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment