78 lines
2.0 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* Module dependencies
*/
2018-06-11 16:55:04 +02:00
/* eslint-disable prefer-template */
// Public node modules.
const isObject = require('lodash/isObject');
2018-06-11 16:55:04 +02:00
const mailgunFactory = require('mailgun-js');
/* 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',
},
mailgun_default_replyto: {
label: 'Mailgun Default Reply-To',
2019-09-27 16:26:09 +02:00
type: 'text',
},
mailgun_api_key: {
label: 'Mailgun API Key',
2019-09-27 16:26:09 +02:00
type: 'text',
},
mailgun_api_host: {
label: 'Mailgun API Host',
2019-09-27 16:26:09 +02:00
type: 'text',
},
mailgun_domain: {
label: 'Mailgun Domain',
2019-09-27 16:26:09 +02:00
type: 'text',
},
},
2019-09-27 16:26:09 +02:00
init: config => {
const mailgun = mailgunFactory({
2018-06-11 16:55:04 +02:00
apiKey: config.mailgun_api_key,
host: config.mailgun_api_host,
domain: config.mailgun_domain,
2019-09-27 16:26:09 +02:00
mute: false,
});
return {
send: (options, cb) => {
return new Promise((resolve, reject) => {
// Default values.
options = isObject(options) ? options : {};
let msg = {
from: options.from || config.mailgun_default_from,
to: options.to,
subject: options.subject,
...(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
};
msg['h:Reply-To'] = options.replyTo || config.mailgun_default_replyto;
2019-09-27 16:26:09 +02: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
});
});
2019-09-27 16:26:09 +02:00
},
};
2019-09-27 16:26:09 +02:00
},
};