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 = {
|
|
|
|
provider: 'mailgun',
|
|
|
|
name: 'Mailgun',
|
|
|
|
auth: {
|
|
|
|
mailgun_default_from: {
|
|
|
|
label: 'Mailgun Default From',
|
2019-09-27 16:26:09 +02:00
|
|
|
type: 'text',
|
2018-05-31 14:29:00 -05:00
|
|
|
},
|
|
|
|
mailgun_default_replyto: {
|
|
|
|
label: 'Mailgun Default Reply-To',
|
2019-09-27 16:26:09 +02:00
|
|
|
type: 'text',
|
2018-05-31 14:29:00 -05:00
|
|
|
},
|
|
|
|
mailgun_api_key: {
|
|
|
|
label: 'Mailgun API Key',
|
2019-09-27 16:26:09 +02:00
|
|
|
type: 'text',
|
2018-05-31 14:29:00 -05:00
|
|
|
},
|
2019-04-02 23:46:49 +03:00
|
|
|
mailgun_api_host: {
|
|
|
|
label: 'Mailgun API Host',
|
2019-09-27 16:26:09 +02:00
|
|
|
type: 'text',
|
2019-04-02 23:46:49 +03:00
|
|
|
},
|
2018-05-31 14:29:00 -05:00
|
|
|
mailgun_domain: {
|
|
|
|
label: 'Mailgun Domain',
|
2019-09-27 16:26:09 +02:00
|
|
|
type: 'text',
|
|
|
|
},
|
2018-05-31 14:29:00 -05:00
|
|
|
},
|
2019-09-27 16:26:09 +02:00
|
|
|
init: config => {
|
2018-05-31 14:29:00 -05:00
|
|
|
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,
|
2019-09-27 16:26:09 +02:00
|
|
|
mute: false,
|
2018-05-31 14:29:00 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
send: (options, cb) => {
|
|
|
|
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 = {
|
2019-11-15 17:49:01 +01:00
|
|
|
from: options.from || config.mailgun_default_from,
|
2018-05-31 14:29:00 -05:00
|
|
|
to: options.to,
|
|
|
|
subject: options.subject,
|
2019-11-15 17:49:01 +01:00
|
|
|
...(options.text && { text: options.text }),
|
|
|
|
...(options.html && { html: options.html }),
|
|
|
|
...(options.template && { template: options.template }),
|
|
|
|
...(options['h:X-Mailgun-Variables'] && {
|
|
|
|
'h:X-Mailgun-Variables': options['h:X-Mailgun-Variables'],
|
|
|
|
}),
|
2019-09-27 16:26:09 +02:00
|
|
|
...(options.attachment && { attachment: options.attachment }),
|
2018-06-11 16:55:04 +02:00
|
|
|
};
|
2019-11-15 17:49:01 +01:00
|
|
|
msg['h:Reply-To'] = options.replyTo || config.mailgun_default_replyto;
|
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
|
|
|
};
|