Skip to content

Instantly share code, notes, and snippets.

@ByteCrak07
Created July 18, 2023 09:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
import { type NextRequest, NextResponse } from 'next/server';
import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';
export async function POST(request: NextRequest) {
const { email, name, message } = await request.json();
const transport = nodemailer.createTransport({
service: 'gmail',
/*
setting service as 'gmail' is same as providing these setings:
host: "smtp.gmail.com",
port: 465,
secure: true
If you want to use a different email provider other than gmail, you need to provide these manually.
Or you can go use these well known services and their settings at
https://github.com/nodemailer/nodemailer/blob/master/lib/well-known/services.json
*/
auth: {
user: process.env.MY_EMAIL,
pass: process.env.MY_PASSWORD,
},
});
const mailOptions: Mail.Options = {
from: process.env.MY_EMAIL,
to: process.env.MY_EMAIL,
// cc: email, (uncomment this line if you want to send a copy to the sender)
subject: `Message from ${name} (${email})`,
text: message,
};
const sendMailPromise = () =>
new Promise<string>((resolve, reject) => {
transport.sendMail(mailOptions, function (err) {
if (!err) {
resolve('Email sent');
} else {
reject(err.message);
}
});
});
try {
await sendMailPromise();
return NextResponse.json({ message: 'Email sent' });
} catch (err) {
return NextResponse.json({ error: err }, { status: 500 });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment