Greetings,
I have encountered a problem with netlify functions, specifically, I’m using nodemailer package to send an email from a website. Everything works fine in development and I’m able to receive mail from the site, but in a production environment, I checked the Network tab and it showing Status code 200, but the response body is empty when it should display the message “Message successfully sent” and the message doesn’t arrive in the inbox.
In netlify i checked the Function log and it displays payload
with data from a form.
ContactForm.js
<Formik
onSubmit={async (values, actions) => {
const options = {
method: 'POST',
body: JSON.stringify(values),
headers: {
'Content-Type': 'application/json'
}
};
await fetch('/.netlify/functions/sendmail', options)
.then((res) => {
actions.setSubmitting(false);
if (res.ok) setIsSubmited(true);
})
catch((err) => {
actions.setSubmitting(false);
});
}}
>
sendmail.js
exports.handler = async (event, context, callback) => {
const payload = JSON.parse(event.body);
const msg = {
to: process.env.MAIL_USERNAME,
from: payload.email,
subject: `New message from ${payload.name} | ${payload.email}`,
text: payload.message
};
const transporter = nodemailer.createTransport({
host: process.env.MAIL_HOST,
port: 587,
secure: false,
auth: {
user: process.env.MAIL_USERNAME,
pass: process.env.MAIL_PASSWORD
},
tls: {
rejectUnauthorized: false
}
});
try {
await transporter.sendMail(msg, (error) => {
if (!error) {
callback(null, { statusCode: 200, body: 'Message successfully sent' });
} else {
callback(null, { statusCode: 400, body: error });
}
});
} catch (e) {
callback({ statusCode: e.code, body: e });
}
};
Please let me know if any other information needs in order to help solve my problem.
Thank you.