UNPKG

@jackdbd/notifications

Version:

Functions used to send notifications to various channels (Telegram, email, etc)

61 lines 1.95 kB
import { debuglog } from 'node:util'; import email from '@sendgrid/mail'; const debug = debuglog('notifications/sendgrid'); const errorMessageFromSendgridApi = ({ err }) => { const errors = err.response.body.errors; const error_messages = errors.map((error) => error.message); const error_message = error_messages.join('; '); switch (err.code) { case 400: case 401: { return error_message; } default: { return error_message; } } }; /** * Send an email to one or more recipients, using the SendGrid API. * * https://docs.sendgrid.com/api-reference/mail-send/mail-send#body */ export const send = async ({ from, html, sendgrid_api_key, subject, to }, options) => { if (!sendgrid_api_key) { throw new Error('sendgrid_api_key not set'); } if (!from) { throw new Error('from not set'); } if (!to) { throw new Error('to not set'); } if (!subject) { throw new Error('subject not set'); } debug('options: %O', options); // https://docs.sendgrid.com/for-developers/sending-email/personalizations // const template_id = options?.template_id email.setApiKey(sendgrid_api_key); const recipient = Array.isArray(to) ? to.join(', ') : to; const data = { from, html, subject, to }; // https://www.twilio.com/blog/sending-bulk-emails-3-ways-sendgrid-nodejs const isMultiple = undefined; try { const [res] = await email.send(data, isMultiple); return { message: `email from ${from} to ${recipient} will be delivered by SendGrid`, sendgrid_message_id: res.headers['x-message-id'] }; } catch (err) { debug(`SendGrid API could not respond. Original error: %O`, err); throw new Error(errorMessageFromSendgridApi({ err: err })); } }; //# sourceMappingURL=sendgrid.js.map