Home
Support Forums

SendGrid not sending from Netlify Functions

Hi,

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.

Hope someone can help
Max

Hi @m_glass,

This is how I did it now to test (and it works):

const sgMail = require('@sendgrid/mail')
exports.handler = async () => {
  sgMail.setApiKey(process.env.SENDGRID_API_KEY)
  return sgMail.send([{
    to: 'receiver1',
    from: 'sender',
    subject: 'subject',
    text: 'body to send'
  },
  {
    to: 'receiver2',
    from: 'sender',
    subject: 'subject',
    text: 'body to send'
  }]).then(() => {
    return {
      statusCode: 200,
      body: JSON.stringify('return something here')
    }
  })
}

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.

Your solution works fine locally but not on production.

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).

2 Likes