Next.js Discord

Discord Forum

Nodemailer issues

Answered
Sokoke posted this in #help-forum
Open in Discord
SokokeOP
Has anyone had a problem where nodemailer works locally but when deployed to vercel it doesn't send the emails and in the logs it returns

Failed to send email: Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at

Here is my nodemailer.tsx

import nodemailer from "nodemailer";
import SMPTransport from "nodemailer-smtp-transport";

export const smtpEmail = process.env.GOOGLE_EMAIL;
export const smtpPassword = process.env.GOOGLE_PASSWORD;

export const transporter = nodemailer.createTransport(
  SMPTransport({
    service: "gmail",
    auth: {
      user: smtpEmail,
      pass: smtpPassword,
    },
  })
);


And here is my contact/routes.tsx
import { NextRequest, NextResponse } from "next/server";

import { render } from "@react-email/components";

import { transporter, smtpEmail } from "@/src/app/components/contact/nodemailer";

import EmailComponent from "@/src/app/components/contact/email";

export async function POST(req: NextRequest, res: NextResponse) {
  const body = await req.json();
  const { name, business, email, message, service } = body;

  const emailHtml = render(
    <EmailComponent name={name} business={business} service={service} email={email} message={message} />
  );

  const options = {
    from: smtpEmail,
    to: smtpEmail,
    subject: "New Contact Message",
    html: emailHtml,
  };

  try {
    // Send email using the transporter
    await transporter.sendMail(options);
  } catch (error) {
    console.error("Failed to send email:", error);
  }
  return new Response("OK");
}


Some information to note:
The environment variables configured in the vercel project's settings are correct
Answered by Sokoke
in the route.tsx
  try {
    // Send email using the transporter
    await transporter.sendMail(options);

was replaced with
    await transporter.sendMail({
      ...options
    });
And it works on vercel
View full answer

4 Replies

SokokeOP
One other thing is nodemailer is sending emails through application specific passwords and not less secure accounts
SokokeOP
I got it working!!!
For anyone who is wondering, geez it was an odd solution, and really I dont understand how it was solved
SokokeOP
in the route.tsx
  try {
    // Send email using the transporter
    await transporter.sendMail(options);

was replaced with
    await transporter.sendMail({
      ...options
    });
And it works on vercel
Answer