#NodeMailer NextJs Api Failing.

1 messages · Page 1 of 1 (latest)

distant kettle
#

I am upgrading from 12 -13 and am unsure how to restructure the contact api call . Any resources very appreciated as I have been searching but coming up blank. Here is the code I have which is not working:
app - api - contact - route.js

import nodemailer from "nodemailer";

export async function POST(req, res) {
  const { name, email, phone, subject, message } = req.json();

  const transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true,
    auth: {
      user: process.env.EMAIL,
      pass: process.env.PASSWORD,
    },
  });

  try {
    const emailRes = await transporter.sendMail({
      from: email,
      // to: process.env.EMAIL,
      to: process.env.EMAIL2,
      subject: `You have a new Contact submission from ${name}`,
      html: `<p>You have a new Radgnarack Contact form submission.</p><br />
      <p><Strong>Name: </Strong> ${name}</p><br />
      <p><Strong>Email: </Strong> ${email}</p><br />
      <p><Strong>Phone: </Strong> ${phone}</p><br />
      <p><Strong>Subject: </Strong> ${subject}</p><br />
      <p><Strong>Message: </Strong> ${message}</p><br />`,
    });
    console.log("Message sent", emailRes.messageId);
  } catch (err) {
    console.log(err);
  }

  res.status(200).json(req.body);
}

Any help appreciated Thank you.

winged geodeBOT
#

Hi @distant kettle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the provided extracts, I don't have enough information to give you a confident answer on how to restructure your contact API call specifically for upgrading from Next.js 12 to 13. However, I can provide you with a general example of how to create an API route in Next.js.

Create a file named contact.js inside the pages/api directory:

import nodemailer from "nodemailer";

export default async function handler(req, res) {
  if (req.method === "POST") {
    const { name, email, phone, subject, message } = req.body;

    const transporter = nodemailer.createTransport({
      host: "smtp.gmail.com",
      port: 465,
      secure: true,
      auth: {
        user: process.env.EMAIL,
        pass: process.env.PASSWORD,
      },
    });

    try {
      const emailRes = await transporter.sendMail({
        from: email,
        to: process.env.EMAIL2,
        subject: `You have a new Contact submission from ${name}`,
        html: `<p>You have a new Radgnarack Contact form submission.</p><br />
        <p><Strong>Name: </Strong> ${name}</p><br />
        <p><Strong>Email: </Strong> ${email}</p><br />
        <p><Strong>Phone: </Strong> ${phone}</p><br />
        <p><Strong>Subject: </Strong> ${subject}</p><br />
        <p><Strong>Message: </Strong> ${message}</p><br />`,
      });
      console.log("Message sent", emailRes.messageId);
    } catch (err) {
      console.log(err);
    }

    res.status(200).json(req.body);
  } else {
    res.status(405).json({ message: "Method not allowed" });
  }
}

This example creates an API route that handles POST requests and sends an email using the provided data. Make sure to compare this example with your current implementation and adjust it accordingly.

#

@distant kettle kapa.ai is still learning and improving, please let us how it did by reacting below

distant kettle
#

@winged geode The above provided answer only shows how it was handled in nextjs12 so it doesn't work in nextjs 13.

winged geodeBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Hmm, I don't know enough to give you a confident answer yet about the specific changes required for Next.js 13 regarding your contact API call. The provided extracts don't contain information about API route changes between Next.js 12 and 13.

However, you can refer to the official Next.js documentation (https://nextjs.org/docs) for more information on API routes and other features.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

Welcome to the Next.js Documentation.

#

@distant kettle kapa.ai is still learning and improving, please let us how it did by reacting below