I have seen other similar posts, but they all seem to be getting an error. I am not getting any error. I have nodemailer set up to send an email when an event is scheduled in a calendar. It works when I run it locally, but in production it does not send the email. I have try catch blocks and console logs in the try blocks and in the catch blocks, and I dont see anything getting logged to the function logs. I added a console log right before the transporter and it does get logged to the function logs, but nothing else after that.
Any help would be much appreciated! My site name is sharp-carson-8083f1.netlify.app/home, and here is the code for it
function sendNewAppointmentEmail(newEvent) {
try {
const startTime = moment(newEvent.start).format('hh:mm A')
const endTime = moment(newEvent.end).format('hh:mm A')
const date = moment(newEvent.start).format('MMM DD YYYY');
const email = newEvent.email === 'undefined' ? '' : newEvent.email;
const phoneNumber = newEvent.phoneNumber === 'undefined' ? '' : newEvent.phoneNumber;
const { EMAIL_ADDRESS, EMAIL_USERNAME, EMAIL_PASSWORD, TO_EMAIL_ADDRESS } = process.env;
const emailData = {
from: EMAIL_ADDRESS,
to: TO_EMAIL_ADDRESS,
subject: 'Capital Cleaners - Nueva Cita',
text: 'Se fijó una nueva cita en el calendario',
html: `<p>Nombre: ${newEvent.firstName + ' ' + newEvent.lastName} </p>
<br/>
<p>Fecha: ${date}</p>
<br/>
<p>Comienzo: ${startTime}</p>
<br/>
<p>Final: ${endTime}</p>
<br/>
<p>Email: ${email}</p>
<br/>
<p>Phone Number: ${phoneNumber}</p>`,
};
console.log({ emailData }) // This does get logged in the function logs
const transporter = nodemailer.createTransport({
port: 465,
host: "smtp.gmail.com",
auth: {
user: EMAIL_USERNAME,
pass: EMAIL_PASSWORD
},
secure: true,
});
const res = transporter.sendMail(emailData, (error, info) => {
try {
if (error) {
console.log({ sendEmailError: error })
return error;
}
console.log({ sendEmailRes: info })
return { message: 'Mail send', message_id: info.messageId };
}
catch (transporterError) {
console.log({ transporterError });
return transporterError;
}
})
return res;
}
catch (error) {
console.log({ sendEmailError })
return error;
}
}
Thanks in advance!