Currently developing a Next.js app that has an API route to send an email. The route has a post that is using Nodemailer & Google OAuth to send an email. Currently when running locally it sends successfully & I receive it. However when deployed on AWS amplify the email is not received successfully however in the network tab in chromes inspector it shows a 200 success.
I am wondering what needs to be done for the email to be received successfully when deployed on AWS amplify & why it’s working locally?
Thank you in advance for any & all assistance;
The app/api/email/route.ts
code file including the post:
import { type NextRequest, NextResponse } from 'next/server';
import nodemailer from 'nodemailer';
const { google } = require('googleapis');
const OAuth2 = google.auth.OAuth2;
export async function POST(request: NextRequest) {
const { email, name, userNote } = await request.json();
// Create Transporter with Google OAuth2
const createTransporter = async () => {
console.log('inside createTransporter ~~~~~~~');
const oauth2Client = new OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
'https://developers.google.com/oauthplayground',
);
// obtain Refresh Token:
oauth2Client.setCredentials({
refresh_token: process.env.GOOGLE_REFRESH_TOKEN,
});
// Obtain Access Token by calling getAccessToken
const accessToken = await new Promise((resolve, reject) => {
oauth2Client.getAccessToken((err, token) => {
if (err) {
console.log('----- Error here cannot getAccessToken boo ------', err);
reject();
}
resolve(token);
});
});
// Create transporter with OAuth2
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: process.env.NODEMAILER_MY_EMAIL,
accessToken,
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
refreshToken: process.env.GOOGLE_REFRESH_TOKEN,
},
});
return transporter;
};
// send secure google email
const sendSecureGoogleMail = async () => {
try {
const mailOptions = {
from: process.env.NODEMAILER_MY_EMAIL,
to: process.env.NODEMAILER_MY_EMAIL,
subject: `???? - ‼ - YO K8 Message from ${name} (${email})`,
text: userNote,
};
let emailTransporter = await createTransporter();
await emailTransporter.sendMail(mailOptions);
} catch (err) {
return NextResponse.json({ error: err }, { status: 500 });
console.log('ERROR: ', err);
}
};
// call send email
try {
await sendSecureGoogleMail();
return NextResponse.json({ message: 'Email sent' });
} catch (err) {
return NextResponse.json({ error: err }, { status: 500 });
}
}
In the browser here is the network tab in chromes inspector: