2017-11-16 16:58:45 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Email.js service
|
|
|
|
*
|
|
|
|
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2018-05-23 00:18:39 -05:00
|
|
|
const config = require('../config/settings.json');
|
|
|
|
|
|
|
|
let mailer;
|
|
|
|
|
|
|
|
if(config.EMAIL_METHOD === 'mailgun') {
|
|
|
|
const mailgun = require('mailgun-js')({
|
|
|
|
apiKey: config.MAILGUN_API_KEY,
|
|
|
|
domain: config.MAILGUN_DOMAIN,
|
|
|
|
mute: false
|
|
|
|
});
|
|
|
|
|
|
|
|
mailer = (msg, mailerCallback) => {
|
|
|
|
// change reply to format for Mailgun
|
|
|
|
msg['h:Reply-To'] = msg.replyTo;
|
|
|
|
mailgun.messages().send(msg, mailerCallback);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if(config.EMAIL_METHOD === 'sendgrid') {
|
|
|
|
const sendgrid = require('@sendgrid/mail');
|
|
|
|
sendgrid.setApiKey(config.SENDGRID_API_KEY);
|
|
|
|
|
|
|
|
mailer = (msg, mailerCallback) => {
|
|
|
|
// change capitalization for SendGrid
|
|
|
|
msg.reply_to = msg.replyTo;
|
|
|
|
sendgrid.send(msg, mailerCallback);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Fallback to default email method
|
|
|
|
const sendmail = require('sendmail')({
|
|
|
|
silent: true
|
|
|
|
});
|
|
|
|
|
|
|
|
mailer = (msg, mailerCallback) => {
|
|
|
|
sendmail(msg, mailerCallback);
|
|
|
|
};
|
|
|
|
}
|
2017-11-16 16:58:45 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2018-05-02 12:28:27 +02:00
|
|
|
send: (options, cb) => { // eslint-disable-line no-unused-vars
|
2017-11-16 16:58:45 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-04 14:00:09 +01:00
|
|
|
// Default values.
|
|
|
|
options = _.isObject(options) ? options : {};
|
2017-12-05 12:10:08 +01:00
|
|
|
options.from = options.from || '"Administration Panel" <no-reply@strapi.io>';
|
2018-01-15 14:50:53 +01:00
|
|
|
options.replyTo = options.replyTo || '"Administration Panel" <no-reply@strapi.io>';
|
2017-12-04 14:00:09 +01:00
|
|
|
options.text = options.text || options.html;
|
|
|
|
options.html = options.html || options.text;
|
2017-11-16 16:58:45 +01:00
|
|
|
|
2018-05-23 00:18:39 -05:00
|
|
|
mailer({
|
2017-12-04 14:00:09 +01:00
|
|
|
from: options.from,
|
|
|
|
to: options.to,
|
2018-01-15 14:50:53 +01:00
|
|
|
replyTo: options.replyTo,
|
2017-12-04 14:00:09 +01:00
|
|
|
subject: options.subject,
|
|
|
|
text: options.text,
|
|
|
|
html: options.html
|
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
reject([{ messages: [{ id: 'Auth.form.error.email.invalid' }] }]);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
2018-05-23 00:18:39 -05:00
|
|
|
});
|
2017-11-16 16:58:45 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|