2016-08-08 11:18:21 +09:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
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';
|
|
|
|
|
2016-06-23 16:42:06 +09:00
|
|
|
NovaEmail.templates = {};
|
2015-12-14 12:43:45 +09:00
|
|
|
|
2016-06-23 16:42:06 +09:00
|
|
|
NovaEmail.addTemplates = function (templates) {
|
|
|
|
_.extend(NovaEmail.templates, templates);
|
2015-12-14 12:43:45 +09:00
|
|
|
};
|
|
|
|
|
2015-05-20 15:33:20 +09:00
|
|
|
// for template "foo", check if "custom_foo" exists. If it does, use it instead
|
2016-06-23 16:42:06 +09:00
|
|
|
NovaEmail.getTemplate = function (templateName) {
|
2015-08-03 15:42:28 +09:00
|
|
|
|
|
|
|
var template = templateName;
|
|
|
|
|
2016-02-23 21:35:54 +09:00
|
|
|
// note: template prefixes are disabled
|
2015-08-03 15:42:28 +09:00
|
|
|
// go through prefixes and keep the last one (if any) that points to a valid template
|
2016-02-23 21:35:54 +09:00
|
|
|
// Telescope.config.customPrefixes.forEach(function (prefix) {
|
2016-06-23 16:42:06 +09:00
|
|
|
// if(typeof NovaEmail.templates[prefix+templateName] === 'string'){
|
2016-02-23 21:35:54 +09:00
|
|
|
// template = prefix + templateName;
|
|
|
|
// }
|
|
|
|
// });
|
2015-08-03 15:42:28 +09:00
|
|
|
|
2015-12-14 12:43:45 +09:00
|
|
|
// return Handlebars.templates[template];
|
|
|
|
|
2016-03-17 16:34:36 +09:00
|
|
|
// console.log(templateName)
|
2016-06-23 16:42:06 +09:00
|
|
|
// console.log(NovaEmail.templates[template])
|
2016-02-23 21:35:54 +09:00
|
|
|
|
2016-06-23 16:42:06 +09:00
|
|
|
return Handlebars.compile(NovaEmail.templates[template], {
|
2016-05-31 10:34:47 +09:00
|
|
|
noEscape: true
|
|
|
|
});
|
2015-08-03 15:42:28 +09:00
|
|
|
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-07-12 17:35:15 +09:00
|
|
|
NovaEmail.buildTemplate = function (htmlContent, optionalProperties = {}) {
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
var emailProperties = {
|
2016-02-17 20:44:52 +09:00
|
|
|
secondaryColor: Telescope.settings.get('secondaryColor', '#444444'),
|
|
|
|
accentColor: Telescope.settings.get('accentColor', '#DD3416'),
|
2016-06-22 18:29:35 +09:00
|
|
|
siteName: Telescope.settings.get('title', "Nova"),
|
2016-02-17 20:44:52 +09:00
|
|
|
tagline: Telescope.settings.get('tagline'),
|
2015-04-22 07:50:11 +09:00
|
|
|
siteUrl: Telescope.utils.getSiteUrl(),
|
|
|
|
body: htmlContent,
|
|
|
|
unsubscribe: '',
|
|
|
|
accountLink: Telescope.utils.getSiteUrl()+'account',
|
2016-02-17 20:44:52 +09:00
|
|
|
footer: Telescope.settings.get('emailFooter'),
|
|
|
|
logoUrl: Telescope.settings.get('logoUrl'),
|
|
|
|
logoHeight: Telescope.settings.get('logoHeight'),
|
2016-07-12 17:35:15 +09:00
|
|
|
logoWidth: Telescope.settings.get('logoWidth'),
|
|
|
|
...optionalProperties
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-06-23 16:42:06 +09:00
|
|
|
var emailHTML = NovaEmail.getTemplate("wrapper")(emailProperties);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-05-31 10:34:47 +09:00
|
|
|
var inlinedHTML = Juice(emailHTML, {preserveMediaQueries: true});
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
var doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
|
|
|
|
|
|
|
|
return doctype+inlinedHTML;
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-06-23 16:42:06 +09:00
|
|
|
NovaEmail.send = function(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
|
|
|
|
|
2016-02-17 20:44:52 +09:00
|
|
|
var from = Telescope.settings.get('defaultEmail', 'noreply@example.com');
|
|
|
|
var siteName = Telescope.settings.get('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.
|
|
|
|
var text = htmlToText.fromString(html, {
|
|
|
|
wordwrap: 130
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('//////// sending email…');
|
|
|
|
console.log('from: '+from);
|
|
|
|
console.log('to: '+to);
|
|
|
|
console.log('subject: '+subject);
|
|
|
|
// console.log('html: '+html);
|
|
|
|
// console.log('text: '+text);
|
|
|
|
|
|
|
|
var email = {
|
|
|
|
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
|
|
|
|
|
|
|
Email.send(email);
|
|
|
|
|
|
|
|
return email;
|
|
|
|
};
|
|
|
|
|
2016-06-23 16:42:06 +09:00
|
|
|
NovaEmail.buildAndSend = function (to, subject, template, properties) {
|
|
|
|
var html = NovaEmail.buildTemplate(NovaEmail.getTemplate(template)(properties));
|
|
|
|
return NovaEmail.send (to, subject, html);
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-06-23 16:42:06 +09:00
|
|
|
NovaEmail.buildAndSendHTML = function (to, subject, html) {
|
|
|
|
return NovaEmail.send (to, subject, NovaEmail.buildTemplate(html));
|
2016-04-13 11:39:01 +09:00
|
|
|
};
|