var util = require('util');
var _ = require('lodash');
var Promise = require('bluebird');
var nodemailer = require('nodemailer');
var sendGridTransport = require('nodemailer-sendgrid-transport');
var BaseMailer = require('./BaseMailer');

util.inherits(SendGridMailer, BaseMailer);

/**
 * Create instance for sending mail through SendGrid API
 * @constructor
 */
function SendGridMailer() {
  BaseMailer.apply(this, arguments);

  this.setTransporter(nodemailer.createTransport(sendGridTransport(this.get('transporter'))));
}

/**
 * Send mail via SendGrid API
 * @param {Object} [_config] Configuration object for overriding
 * @returns {Promise}
 */
SendGridMailer.prototype.send = function (_config) {
  var config = _.omit(_.merge({}, this.get(), _config), 'transporter');

  return new Promise(function (resolve, reject) {
    this.getTransporter().sendMail(config, function (error, result) {
      return error ? reject(error) : resolve(result);
    });
  }.bind(this));
};

module.exports = SendGridMailer;

import _ from 'lodash';
import nodemailer from 'nodemailer';
import sendGridTransport from 'nodemailer-sendgrid-transport';
import BaseMailer from './BaseMailer';

export default class SendGridMailer extends BaseMailer {
  constructor(...args) {
    super(...args);

    this.setProvider(nodemailer.createTransport(sendGridTransport(this.get('provider'))));
  }

  /**
   * Send mail
   * @param {Object} [_config] Additional configuration for overriding
   * @returns {Promise}
   */
  send(_config) {
    let config = _.omit(_.merge({}, this.get(), _config), 'provider');

    return new Promise((resolve, reject) => {
      this.getProvider().sendMail(config, (error, result) => error ? reject(error) : resolve(result));
    });
  }
}

Rewrite SendGridMailer with ES6