mirror of
https://github.com/strapi/strapi.git
synced 2025-08-14 03:34:53 +00:00
78 lines
1.9 KiB
JavaScript
78 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Module dependencies
|
|
*/
|
|
|
|
/* eslint-disable prefer-template */
|
|
// Public node modules.
|
|
const _ = require('lodash');
|
|
const mailgunFactory = require('mailgun-js');
|
|
|
|
/* 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',
|
|
},
|
|
mailgun_api_host: {
|
|
label: 'Mailgun API Host',
|
|
type: 'text',
|
|
},
|
|
mailgun_domain: {
|
|
label: 'Mailgun Domain',
|
|
type: 'text',
|
|
},
|
|
},
|
|
init: config => {
|
|
const mailgun = mailgunFactory({
|
|
apiKey: config.mailgun_api_key,
|
|
host: config.mailgun_api_host,
|
|
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,
|
|
html: options.html,
|
|
...(options.attachment && { attachment: options.attachment }),
|
|
};
|
|
msg['h:Reply-To'] = options.replyTo;
|
|
|
|
mailgun.messages().send(msg, function(err) {
|
|
if (err) {
|
|
reject([{ messages: [{ id: 'Auth.form.error.email.invalid' }] }]);
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
},
|
|
};
|
|
},
|
|
};
|