Sendgrid not sending email in Production (ENVIRONMENT VARIABLE ISSUE?)

Netlify Site Name: or you can go directly to Assessment | Org Dev Group - Org Dev Group

I am running a NextJS site with my first time being on Netlify. In our website, we have an assessment users can take which at the last step takes their form information and runs it by a Netlify Function (pdf.js) to create a PDF report and send it via email using SENDGRID_API_KEY.

I have SENDGRID_API_KEY in .env.local which works in local environment. For Netlify, I went to the Environment Variables section and also added the API key there.

When I run the assessment and submit a form, I don’t see any errors in the console but the PDF email does not get sent. In my dev environment, however, it does get sent.

I have checked SENDGRID and made sure IP Access and all is not restricted.
With Vercel, I normally just add the environment variable similarly like Netlify has it and it works fine there, so I’m having trouble understanding why Netlify is different.

Can you recommend me what to do in this case to troubleshoot? I looked at various topics similar to this but I either did not understand them or tried their solution and it ended up not working for me.

Thank you so much.

Hey all,

So after hours of working through this and searching, I ended up doing the following:

  • After learning that Netlify Functions can have the API Key listed in there, I no longer went with the environment variables as someone had written it’s only useful for the build, and once the build is done, the environment variable will not be available. I instead pasted the API key directly into the Netlify Function for SendGrid.

  • I saw someone posted that their SendGrid was not sending emails on Production, so I kept searching and found to use sendgrid in an async function and use await to send the email, which it did send that email:

      await sgMail.send(msg);
      return { statusCode: 200 };

Hi @OrgDevGroup :wave:t6: ,

Welcome to the forums and thanks so much for stopping by. :sparkles:

Thank you for sharing your solution! This is definitely helpful for other users