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 EMAIL ="myMail";
const PASSWORD="appPassword";
const transport = nodemailer.createTransport({
//setting service as 'gmail' is same as providing these setings:
host: "smtp.mail.yahoo.com",
port: 465,
secure: true,
service: 'yahoo',
auth: {
user: EMAIL,
pass: PASSWORD,
},
});
const mailOptions: Mail.Options = {
from: process.env.MY_EMAIL,
to: process.env.MY_EMAIL,
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 });
}
}
I am new to Next.js and I am getting an error when I send my form, it's giving me a 500 Server error. I am trying to go through yahoo as my mail provider I have created an app password, I can't work out what is going wrong. I'm wondering is the credentials my end or am I missing something in the API? Your help would be greatly appreciated. Here a some screenshots of my code.[[[[[enter image description hereenter image description here](https://i.sstatic.net/pg1eFSfg.png)](https://i.sstatic.net/ztxRRE5n.png)](https://i.sstatic.net/4hdUOR9L.png)](https://i.sstatic.net/CbdjU9Fr.png)](https://i.sstatic.net/AdKHNV8J.png)
So I have tried to use gmail and yahoo, but I still have the same problem.