-2

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.

New contributor
Stephen Welch is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • Welcome. Just a side note: using images for code or errors causes a lot of problems. Use images only to illustrate graphical issues. Please read Why should I not upload images of code/data/errors?. Thank you
    – pierpy
    Commented Jul 5 at 6:25
  • Can you please share more information from the backend side code as well? How do you configure the node mailer? Also please verify whether this 500 error is from your server or from your frontend it self. Commented Jul 5 at 7:00
  • The error is coming from the front-end, once I fire the button in the console it's logged as a 500 server error. Here is the code for the server side. It's posted at the top of the message. Commented Jul 5 at 7:16

0

Browse other questions tagged or ask your own question.