Source: mail/index.js

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