2018-05-31 14:29:00 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
2018-06-11 16:55:04 +02:00
|
|
|
/* eslint-disable prefer-template */
|
2018-05-31 14:29:00 -05:00
|
|
|
// Public node modules.
|
2019-11-15 17:49:01 +01:00
|
|
|
const isObject = require('lodash/isObject');
|
2018-06-11 16:55:04 +02:00
|
|
|
const mailgunFactory = require('mailgun-js');
|
|
|
|
|
2018-05-31 14:29:00 -05:00
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
module.exports = {
|
2019-09-27 16:26:09 +02:00
|
|
|
init: config => {
|
2018-05-31 14:29:00 -05:00
|
|
|
const mailgun = mailgunFactory({
|
2020-05-14 11:26:19 +02:00
|
|
|
apiKey: config.apiKey,
|
|
|
|
host: config.apiHost,
|
|
|
|
domain: config.domain,
|
2019-09-27 16:26:09 +02:00
|
|
|
mute: false,
|
2018-05-31 14:29:00 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2020-03-21 02:02:47 +13:00
|
|
|
send: options => {
|
2018-05-31 14:29:00 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Default values.
|
2019-11-15 17:49:01 +01:00
|
|
|
options = isObject(options) ? options : {};
|
2018-05-31 14:29:00 -05:00
|
|
|
|
|
|
|
let msg = {
|
2020-05-14 11:26:19 +02:00
|
|
|
from: options.from || config.defaultFrom,
|
2018-05-31 14:29:00 -05:00
|
|
|
to: options.to,
|
2020-05-14 11:26:19 +02:00
|
|
|
cc: options.cc,
|
|
|
|
bcc: options.bcc,
|
2018-05-31 14:29:00 -05:00
|
|
|
subject: options.subject,
|
2020-05-14 11:26:19 +02:00
|
|
|
'h:Reply-To': options.replyTo || config.defaultReplyTo,
|
|
|
|
text: options.text,
|
|
|
|
html: options.html,
|
|
|
|
template: options.template,
|
|
|
|
'h:X-Mailgun-Variables': options['h:X-Mailgun-Variables'],
|
|
|
|
attachment: options.attachment,
|
2018-06-11 16:55:04 +02:00
|
|
|
};
|
2018-05-31 14:29:00 -05:00
|
|
|
|
2019-09-27 16:26:09 +02:00
|
|
|
mailgun.messages().send(msg, function(err) {
|
2018-05-31 14:29:00 -05:00
|
|
|
if (err) {
|
|
|
|
reject([{ messages: [{ id: 'Auth.form.error.email.invalid' }] }]);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
2018-06-11 16:55:04 +02:00
|
|
|
});
|
2018-05-31 14:29:00 -05:00
|
|
|
});
|
2019-09-27 16:26:09 +02:00
|
|
|
},
|
2018-05-31 14:29:00 -05:00
|
|
|
};
|
2019-09-27 16:26:09 +02:00
|
|
|
},
|
2018-05-31 14:29:00 -05:00
|
|
|
};
|