Home > Net >  async await not working if sendgrid failed to sent email
async await not working if sendgrid failed to sent email

Time:12-03

I have a question. I am trying to sent the user a email when they have created an account.

But when Sendgrid fails to sent the email the async await function didn't notice that and the code runs further.

This is my email module

const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
 
   module.exports = {
        FreshleafVerificationEmail: async(receiver) => {
            const msg = {
                to: receiver,
                from: '[email protected]',
                subject: 'Verifieer uw email adres - FreshLeaf',
                text: 'Verifieer uw email adres door te klikken op de onderstaande link'
            }
            await sgMail
            .send(msg)
            .then((response) => {
              console.log(response[0].statusCode)
              console.log(response[0].headers)
            })
            .catch((error) => {
              console.log(error)
            })
        }
    }

And this is where the method gets executed

    try {
        //Save settings and user in the database
        await FreshleafVerificationEmail(newUser.email)
        await newSetting.save();
        const user = await newUser.save();
        res.status(201).json({message: 'Er is een nieuwe gebruiker aangemaakt', user: user})
    } 
    catch(error) {
        res.status(400).json({message: 'Er is een probleem opgetreden', error: error})
    }

Can someone help me with this problem

CodePudding user response:

I agree that the answer by @Matt covers the error situation when Sendgrid does not accept the email. As @O.Jones does suggest, the API would be the way to go. I do not work with this particular library, but Sendgrid should return a uniq ID of the email if it was accepted. That can be used to search for the status of the email via API.

Sendgrid provides multiple statuses: https://docs.sendgrid.com/ui/analytics-and-reporting/email-activity-feed You should look for Delivered or in case of Deferred you should wait more.

I assume that this may be a bad news, but Sendgrid copies hear a behavior of any other standard email server. With a simpler interface.

CodePudding user response:

The catch is handling the error condition and allows the code to continue. If you want other error handlers to see the error then it needs to throw again:

        FreshleafVerificationEmail: async(receiver) => {
            const msg = {
                to: receiver,
                from: '[email protected]',
                subject: 'Verifieer uw email adres - FreshLeaf',
                text: 'Verifieer uw email adres door te klikken op de onderstaande link'
            }
            try {
                const response = await sgMail.send(msg)
                console.log('Email sent', response[0].statusCode, response[0].headers)
            }
            catch (error) {
                console.log('Email failed to send', msg, error)
                throw error
            }
        }
  • Related