You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Within a serverless function such as Vercel it's nice to encapsulate the sending function within a promise to ensure that it completes before the job is terminated. Also checking for an API key in case it's not added by mistake
Code Snippet
constsgMail=require('@sendgrid/mail');sgMail.setApiKey(process.env.SENDGRID_API_KEY);if(!process.env.SENDGRID_API_KEY){thrownewError("Missing API key");}constmsg={to: '[email protected]',from: '[email protected]',// Use the email address or domain you verified abovesubject: '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>',};constsendMail=(msg)=>{returnnewPromise((resolve,reject)=>{sgMail.send(msg).then(response=>{resolve(response);},error=>{reject(error);});});};//ES6sendMail(msg).then(()=>{},error=>{console.error(error);if(error.response){console.error(error.response.body)}});//ES8(async()=>{try{awaitsendMail(msg);}catch(error){console.error(error);if(error.response){console.error(error.response.body)}}})();
Technical details:
sendgrid mail: ^7.4.6
node version: 14.x
The text was updated successfully, but these errors were encountered:
Issue Summary
Within a serverless function such as Vercel it's nice to encapsulate the sending function within a promise to ensure that it completes before the job is terminated. Also checking for an API key in case it's not added by mistake
Code Snippet
Technical details:
The text was updated successfully, but these errors were encountered: