75 lines
1.9 KiB
JavaScript
Raw Normal View History

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');
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 = {
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 : {};
options.from = options.from || '"Administration Panel" <no-reply@strapi.io>';
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
mailer({
2017-12-04 14:00:09 +01:00
from: options.from,
to: options.to,
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();
}
});
2017-11-16 16:58:45 +01:00
});
}
};