2018-05-31 14:29:00 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
2018-06-11 16:55:04 +02:00
|
|
|
/* eslint-disable import/no-unresolved */
|
|
|
|
/* eslint-disable prefer-template */
|
2018-05-31 14:29:00 -05:00
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
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 = {
|
|
|
|
provider: 'mailgun',
|
|
|
|
name: 'Mailgun',
|
|
|
|
auth: {
|
|
|
|
mailgun_default_from: {
|
|
|
|
label: 'Mailgun Default From',
|
|
|
|
type: 'text'
|
|
|
|
},
|
|
|
|
mailgun_default_replyto: {
|
|
|
|
label: 'Mailgun Default Reply-To',
|
|
|
|
type: 'text'
|
|
|
|
},
|
|
|
|
mailgun_api_key: {
|
|
|
|
label: 'Mailgun API Key',
|
|
|
|
type: 'text'
|
|
|
|
},
|
2019-04-02 23:46:49 +03:00
|
|
|
mailgun_api_host: {
|
|
|
|
label: 'Mailgun API Host',
|
|
|
|
type: 'text'
|
|
|
|
},
|
2018-05-31 14:29:00 -05:00
|
|
|
mailgun_domain: {
|
|
|
|
label: 'Mailgun Domain',
|
|
|
|
type: 'text'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
init: (config) => {
|
|
|
|
|
|
|
|
const mailgun = mailgunFactory({
|
2018-06-11 16:55:04 +02:00
|
|
|
apiKey: config.mailgun_api_key,
|
2019-04-02 23:46:49 +03:00
|
|
|
host: config.mailgun_api_host,
|
2018-05-31 14:29:00 -05:00
|
|
|
domain: config.mailgun_domain,
|
|
|
|
mute: false
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
send: (options, cb) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Default values.
|
|
|
|
options = _.isObject(options) ? options : {};
|
|
|
|
options.from = options.from || config.mailgun_default_from;
|
|
|
|
options.replyTo = options.replyTo || config.mailgun_default_replyto;
|
|
|
|
options.text = options.text || options.html;
|
|
|
|
options.html = options.html || options.text;
|
|
|
|
|
|
|
|
let msg = {
|
|
|
|
from: options.from,
|
|
|
|
to: options.to,
|
|
|
|
subject: options.subject,
|
|
|
|
text: options.text,
|
2019-01-17 10:55:29 -06:00
|
|
|
html: options.html,
|
|
|
|
...(options.attachment && { attachment: options.attachment })
|
2018-06-11 16:55:04 +02:00
|
|
|
};
|
|
|
|
msg['h:Reply-To'] = options.replyTo;
|
2018-05-31 14:29:00 -05:00
|
|
|
|
|
|
|
mailgun.messages().send(msg, function (err) {
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|