2016-06-23 16:42:06 +09:00
|
|
|
import NovaEmail from '../namespace.js';
|
2016-05-20 09:59:01 +09:00
|
|
|
import Juice from 'juice';
|
|
|
|
import htmlToText from 'html-to-text';
|
|
|
|
import Handlebars from 'handlebars';
|
2017-02-02 16:18:33 +01:00
|
|
|
import { Utils, getSetting } from 'meteor/nova:lib'; // import from nova:lib because nova:core is not loaded yet
|
2016-05-20 09:59:01 +09:00
|
|
|
|
2016-06-23 16:42:06 +09:00
|
|
|
NovaEmail.templates = {};
|
2015-12-14 12:43:45 +09:00
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
NovaEmail.addTemplates = templates => {
|
2016-06-23 16:42:06 +09:00
|
|
|
_.extend(NovaEmail.templates, templates);
|
2015-12-14 12:43:45 +09:00
|
|
|
};
|
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
NovaEmail.getTemplate = templateName => Handlebars.compile(
|
|
|
|
NovaEmail.templates[templateName],
|
|
|
|
{ noEscape: true}
|
|
|
|
);
|
2015-08-03 15:42:28 +09:00
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
NovaEmail.buildTemplate = (htmlContent, optionalProperties = {}) => {
|
2015-08-03 15:42:28 +09:00
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
const emailProperties = {
|
2016-12-12 15:00:56 +09:00
|
|
|
secondaryColor: getSetting('secondaryColor', '#444444'),
|
|
|
|
accentColor: getSetting('accentColor', '#DD3416'),
|
|
|
|
siteName: getSetting('title', "Nova"),
|
|
|
|
tagline: getSetting('tagline'),
|
2016-12-12 11:34:28 +09:00
|
|
|
siteUrl: Utils.getSiteUrl(),
|
2015-04-22 07:50:11 +09:00
|
|
|
body: htmlContent,
|
|
|
|
unsubscribe: '',
|
2016-12-12 11:34:28 +09:00
|
|
|
accountLink: Utils.getSiteUrl()+'account',
|
2016-12-12 15:00:56 +09:00
|
|
|
footer: getSetting('emailFooter'),
|
|
|
|
logoUrl: getSetting('logoUrl'),
|
|
|
|
logoHeight: getSetting('logoHeight'),
|
|
|
|
logoWidth: getSetting('logoWidth'),
|
2016-07-12 17:35:15 +09:00
|
|
|
...optionalProperties
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
const emailHTML = NovaEmail.getTemplate("wrapper")(emailProperties);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
const inlinedHTML = Juice(emailHTML, {preserveMediaQueries: true});
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
const doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
return doctype+inlinedHTML;
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
NovaEmail.send = (to, subject, html, text) => {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
// TODO: limit who can send emails
|
|
|
|
// TODO: fix this error: Error: getaddrinfo ENOTFOUND
|
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
const from = getSetting('defaultEmail', 'noreply@example.com');
|
|
|
|
const siteName = getSetting('title', 'Telescope');
|
2015-05-01 18:22:00 +02:00
|
|
|
subject = '['+siteName+'] '+subject;
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2015-05-01 18:22:00 +02:00
|
|
|
if (typeof text === 'undefined'){
|
2015-04-22 07:50:11 +09:00
|
|
|
// Auto-generate text version if it doesn't exist. Has bugs, but should be good enough.
|
2016-11-15 09:02:30 +01:00
|
|
|
text = htmlToText.fromString(html, {
|
2015-04-22 07:50:11 +09:00
|
|
|
wordwrap: 130
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log('//////// sending email…'); // eslint-disable-line
|
|
|
|
console.log('from: '+from); // eslint-disable-line
|
|
|
|
console.log('to: '+to); // eslint-disable-line
|
|
|
|
console.log('subject: '+subject); // eslint-disable-line
|
2015-04-22 07:50:11 +09:00
|
|
|
// console.log('html: '+html);
|
|
|
|
// console.log('text: '+text);
|
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
const email = {
|
2015-04-22 07:50:11 +09:00
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
subject: subject,
|
|
|
|
text: text,
|
|
|
|
html: html
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-08-26 10:16:01 +09:00
|
|
|
try {
|
|
|
|
Email.send(email);
|
|
|
|
} catch (error) {
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log("// error while sending email:"); // eslint-disable-line
|
|
|
|
console.log(error); // eslint-disable-line
|
2016-08-26 10:16:01 +09:00
|
|
|
}
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
return email;
|
2016-08-26 10:16:01 +09:00
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
};
|
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
NovaEmail.buildAndSend = (to, subject, template, properties) => {
|
|
|
|
const html = NovaEmail.buildTemplate(NovaEmail.getTemplate(template)(properties));
|
2016-06-23 16:42:06 +09:00
|
|
|
return NovaEmail.send (to, subject, html);
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2017-02-16 10:04:00 +01:00
|
|
|
NovaEmail.buildAndSendHTML = (to, subject, html) => NovaEmail.send(
|
|
|
|
to,
|
|
|
|
subject,
|
|
|
|
NovaEmail.buildTemplate(html)
|
|
|
|
);
|