I’m having trouble understanding how to call multiple async functions (either one after another or simultaneously works).
I have two functions that use nodemailer to send out an email. First to myself and the other to the user, then return status.
The following code is the closest I’ve gotten and works on dev, but not on prod. I’m guessing it’s similar to this post here, but different enough that I couldn’t seem to use it by simply assigning the functions to a variable. Unfortunately, the post at the bottom’s link that would delve into more detail is not found.
exports.handler = async (event, context) => {
let body = JSON.parse(event.body).payload;
const email = body.email;
const name = body.name;
const message = body.data.message;
var transporter = nodemailer.createTransport({
service: 'gmail',
port: 587,
requireTLS: true,
auth: {
user: GMAIL,
pass: EMAIL_PW,
}
});
await mailSelf(transporter, email, name, message);
await mailReply(transporter, email); //mailReply is the same as mailSelf but different mailOptions
return {statusCode: 200, body: "success!"};
}
async function mailSelf(transporter, email, name, message) {
let mailOptions = {
from: GMAIL,
to: HOTMAIL,
subject: `name`,
html: `<h3>Message:</h3><p>${message}</p> from <p>${email}</p>`
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log('error message', error);
//resolve(false)
} else {
console.log('Email sent: ' + info.response);
//resolve(true);
}
});
}
I’ve tried to set them to promises with returning resolve(true) and using
Promise.all(promises).then(return {statusCode: 200, body: "responseBody"}
and I’ve also tried passing and using callback functions instead, but both methods end up with a 500 response with “lambda response was undefined. check your function code again”. Both methods will usually seem to send the emails out, though.
Any help is much appreciated, thanks!