The send
and sendMultiple
methods return a Promise
, so you can handle success and capture errors:
sgMail
.send(msg)
.then(() => {
// Celebrate
})
.catch(error => {
// Log friendly error
console.error(error);
if (error.response) {
// Extract error msg
const {message, code, response} = error;
// Extract response msg
const {headers, body} = response;
console.error(body);
}
});
Alternatively, pass a callback function as the last parameter:
sgMail
.send(msg, (error, result) => {
if (error) {
// Do something with the error
}
else {
// Celebrate
}
});