server/helpers/mailer.js

"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _config = _interopRequireDefault(require("../../common/helpers/config.js"));
var _nodemailer = _interopRequireDefault(require("nodemailer"));
/*
 * Copyright (C) 2021  Divyanshu Raj
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

var mailConfig = _config.default.mailConfig;

/**
 * This is a dedicated mailing interface for bookbrainz mailing services.
 *
 * @param {string} from - Email address of sender
 * @param {string} to - Email address of receiver
 * @param {string} subject- Subject of the E-mail
 * @param {string} html - Body of E-mail in form of HTML
 * @returns {Promise<object>} Returns a promise that resolves with an error object if message failed or an info object if the message succeeded (see https://nodemailer.com/usage/ for more details)
 * If entity is found successfully in the database this function set the entity data
 * at res.locals.entity and return to next function.
 * @example
 * Promise way :
 *
 * sendEmail("abc@gmail.com", "def@gmail.com", "Revision added to Entity", "some html string")
 * .then((response)=>{
 * // the response object contains details of a successful response (see https://nodemailer.com/usage)
 * console.log("Email has been sent successfully")
 * })
 * .catch((error)=>{
 * //handle error
 * })
 *
 * Async/Await way :
 *
 * try{
 * const response = await sendEmail("abc@gmail.com", "def@gmail.com", "Revision added to Entity", "some html string")
 * console.log("Email has been sent successfully")
 * }
 * catch (error) {
 * //handle the error
 * }
 * @description
 * This helper function  sends E-mail with attributes collected from argument in given order.
 * It uses nodemailer's transporter to send the E-mail under the hood.
 * It returns a promise that should be handled using .then()/.catch() or async/await syntax wrapped in a try…catch block.
 */
function sendEmail(_x, _x2, _x3, _x4) {
  return _sendEmail.apply(this, arguments);
}
function _sendEmail() {
  _sendEmail = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(from, to, subject, html) {
    var transporter;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            transporter = _nodemailer.default.createTransport(mailConfig); // returns a promise's resolved value or throws an error
            _context.next = 3;
            return transporter.sendMail({
              from: from,
              html: html,
              subject: subject,
              to: to
            });
          case 3:
            return _context.abrupt("return", _context.sent);
          case 4:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _sendEmail.apply(this, arguments);
}
var _default = exports.default = sendEmail;
//# sourceMappingURL=mailer.js.map