sendgrid / sendgrid-nodejs

The Official Twilio SendGrid Led, Community Driven Node.js API Library
https://sendgrid.com
MIT License
3k stars 782 forks source link

send command not fired #1356

Closed Nico-L closed 2 years ago

Nico-L commented 2 years ago

Issue Summary

Using @sendgrid/email in a Netlify function does not send the email. The send(msg) command returns neither a success nor an error. Nothing seems to be fired.

Steps to Reproduce

  1. Setup a Netlify Function with the "hello email" example provided by sendgrid
  2. Deploy the site to Netlify
  3. fire the Netlify function

Code Snippet

 const sendgrid = require('@sendgrid/mail');
 sendgrid.setApiKey(process.env.SENDGRID_API_KEY);

 const handler = async (event) => {
const msg = {
  to: 'nicolas@luchier.fr',
  from: 'nicolas@luchier.fr', // Use the email address or domain you verified above
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};

sendgrid
  .send(msg)
  .then(() => {console.log('hop !')}, error => {
    console.log(error);

    if (error.response) {
      console.log(error.response.body)
    }
  });
    return {
        statusCode: 200,
        body: JSON.stringify({ retour: "ok"}),
        // // more keys you can return:
        // headers: { "headerName": "headerValue", ... },
        // isBase64Encoded: true,
      }
    }

    module.exports = { handler }

I verified that the SENDGRID_API_KEY has the correct key. No error fired but no email sent. It works in local development... I am not sure of the difference.

Exception/Log

No log output, neither console.log("hop") nor the error message

Technical details:

Any help would be really appreciated. I am also posting this to the Netlify forum.

Thanks, Nicolas

Nico-L commented 2 years ago

Hello,

It was a promise not correctly dealt with.