All files / modules/mail index.js

100% Statements 9/9
100% Branches 4/4
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41      8x 8x     8x                       8x 2x 2x 2x             2x               8x      
/**
 * @module mail
 */
const sgMail = require('@sendgrid/mail');
const log = require("../../util/log");
 
// Configure interface
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
 
/**
 * Send email asynchronously.
 * Supports both HTML and plain text email bodies.
 * Success or failure is logged.
 * @param {String} from
 * @param {String} to
 * @param {String} subject
 * @param {String} text
 * @param {String} [html=text] text will be used as html body if omitted
 */
const sendEmail = async (from, to, subject, text, html) => {
    log.info("'%s': Sending email to '%s'", from, to);
    try {
        await sgMail.send({
            from: from ? from : process.env.DEFAULT_EMAIL_ADDRESS,
            to,
            subject,
            text,
            html: html ? html : text,
        });
        log.info("'%s': Email sent successfully to '%s'", from, to);
    } catch (error) {
        /* istanbul ignore next */
        log.error("'%s': Failed sending email to '%s', err: " +
            error.message, from, to);
    }
};
 
module.exports = {
    sendEmail,
};