so I created a function that sends an email using the SendGrid library. Everything is working fine locally with the Netlify CLI and all emails arrive. But as soon as I push my code and the function gets deployed live, the function executes but no email is arriving. I already described the issue here.
I already discovered this thread. But I don’t know exactly which IP I should add.
You can always split the emails out in a different array like const emails = [{email1}, {email2}] and use return sgMail.send(emails). I just showed a simple example. You can and should use the .catch() block to handle errors too.
Note: The sender email address must be a verified sender configured here: SendGrid.
Okay I got the solution: log in to SendGrid, go to IP Access Management and disable your IP Allow List (when this list is activated, only IP addresses listed in there are granted access - including API requests).
I would like to send two emails but just trying to successfully send the one right now - the second email code is commented out. Thanks for the quick reply @hrishikesh!
const client = require('@sendgrid/mail');
const {
SENDGRID_API_KEY,
SENDGRID_FROM_EMAIL,
} = process.env;
exports.handler = async function (event, context, callback) {
const payload = JSON.parse(event.body).payload.data;
const volunteerEmail = payload.email;
const volunteerName = payload.fullname;
const volunteerPronouns = payload.genderpronouns;
if (volunteerPronouns == null) {
volunteerPronouns = 'They/Them/Their'
}
const branchArr = payload.branch.split(",");
const branchEmail = branchArr[0];
const branchName = branchArr[1];
client.setApiKey(SENDGRID_API_KEY);
const msgToBranch = {
to: `${branchEmail}`,
from: SENDGRID_FROM_EMAIL,
subject: "You have a new volunteer!",
text: `${volunteerName} wants to join your branch.`,
html: `<div>🌈</div>`,
};
// const msgToVolunteer = {
// to: `${volunteerEmail}`,
// from: SENDGRID_FROM_EMAIL,
// subject: `Thanks for reaching out to Sexpression:${branchName}`,
// text: `The branch committee member of Sexpression:${branchName} will reach out to you shortly.`,
// };
try {
let response1 = await client.send(msgToBranch);
// let response2 = await client.send(msgToVolunteer);
console.log(response1);
return {
statusCode: 200,
body: JSON.stringify({ msg: response1}),
};
} catch (err) {
return {
statusCode: err.code,
body: JSON.stringify({ msg: err.message }),
};
}
};
Hi all - sorry to resurrect this, but this is post is the top hit when searching for SendGrid and Netlify problems. None of these solutions worked for me, but after a lot of trial and error I hit on a solution that may or may not help you.
My solution was making sure that the function and Sendmail’s send was async. Here is my full code that I used to integrate a Stripe webhook with Netflify functions. I’m not a real developer so apologies if it’s a mess, but this works for me.
I had a confusing issue where sometimes it would, and sometimes it would not send emails while locally everything worked.
Finally I noticed the in the Functions logs that the methods for sending emails were called but not finished. Weirdly when I’d do an unrelated REST call ten minutes later the emails would get sent.
Netlify seemed to pause and resume processes.
The emails were sent through a callstack of about five methods. In one of these I had forgotten to await the response.
So remember: when using promises or async/await, you’ll have to use them all the way down!