I’ve created a function which send email from a form on the website through mailjet.
The function works when I use netlify dev locally but does not work from deploy preview… Don’t know what goes wrong. The problem is that request to mailjet never seeme to be run. the console.logs in the promise or catch is not outputed. When run in Netlify dev those are outputed and an email is sent
Here is the code from the function I’m calling:
exports.handler = async function(event, context) {
const mailjet = require("node-mailjet").connect(process.env.MJ_APIKEY_PUBLIC,
process.env.MJ_APIKEY_PRIVATE);
body = JSON.parse(event.body);
console.log(body);
console.log(process.env.MAIL_FROM_ADDRESS);
console.log(process.env.MAIL_TO_ADDRESS);
const request = mailjet.post("send", { version: "v3.1" }).request({
Messages: [
{
From: {
Email: process.env.MAIL_FROM_ADDRESS,
Name: body.name.value,
},
To: [
{
Email: process.env.MAIL_TO_ADDRESS,
Name: "from Sender",
},
],
Subject: body.title.value,
TextPart: body.message.value,
CustomID: "KontaktOss",
// HTMLPart: '<h3>Dear passenger 1, welcome to <a href="https://www.mailjet.com/">Mailjet</a>!
</h3><br />May the delivery force be with you!',
},
],
});
console.log("sending mail");
request
.then((result) => {
console.log("resultat fra mailjet: " + JSON.stringify(result.body));
})
. catch((err) => {
console.error(JSON.stringify(err));
});
console.log("line 33");
return {
statusCode: 200,
body: JSON.stringify({ message: "Mailen er sendt" }),
};
};