Next.js Discord

Discord Forum

cant get nodemailer working

Unanswered
Sloughi posted this in #help-forum
Open in Discord
SloughiOP
here is app/send-email/route.ts
import nodemailer from 'nodemailer';

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

  //  transporter
  const transporter = nodemailer.createTransport({
    service: 'gmail', 
    auth: {
      user: process.env.EMAIL_USER, 
      pass: process.env.EMAIL_PASS, 
    },
  });

  const mailOptions = {
    from: email,
    to: 'example@gmail.com', // recipient email
    subject: 'New Contact Form Submission',
    text: `Name: ${name}\nEmail: ${email}\nMessage: ${message}`,
  };

  // send email
  try {
    await transporter.sendMail(mailOptions);
    return new Response(JSON.stringify({ success: true }), { status: 200 });
  } catch (error) {
    console.error('Error sending email:', error);
    return new Response(JSON.stringify({ error: 'Failed to send email' }), { status: 500 });
  }
}

13 Replies

SloughiOP
and here is my app/contact/page.tsx
anyone have experience with nodemailer or know why this might not be working?
i tried turning on "Allow users to manage their access to less secure apps" in gmail settings
also tried using an app password
are you getting any errors?
SloughiOP
no errors. i just never get the email
you prolly should get an error, if not, it's most likely issues in your smtp server. I recently didn't get the mails in gmail too, cause my mail formatting wasn't proper. might wanna check that out once
SloughiOP
got it, thanks
@Sloughi where are you running this on?
Vercel?
SloughiOP
not deployed yet
i found the issue though. my send-email route was in a folder that was in the api folder
took awhile to realize cause i wasnt even getting 404