2016-04-01 12:38:50 +09:00
|
|
|
Telescope.email.templates = {};
|
2015-12-14 12:43:45 +09:00
|
|
|
|
2016-04-01 12:38:50 +09:00
|
|
|
Telescope.email.addTemplates = function (templates) {
|
|
|
|
_.extend(Telescope.email.templates, templates);
|
2015-12-14 12:43:45 +09:00
|
|
|
};
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
var htmlToText = Npm.require('html-to-text');
|
|
|
|
|
2015-05-20 15:33:20 +09:00
|
|
|
// for template "foo", check if "custom_foo" exists. If it does, use it instead
|
2016-04-01 12:38:50 +09:00
|
|
|
Telescope.email.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-04-01 12:38:50 +09:00
|
|
|
// if(typeof Telescope.email.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-04-01 12:38:50 +09:00
|
|
|
// console.log(Telescope.email.templates[template])
|
2016-02-23 21:35:54 +09:00
|
|
|
|
2015-12-14 12:43:45 +09:00
|
|
|
return function (properties) {
|
2016-04-01 12:38:50 +09:00
|
|
|
return Spacebars.toHTML(properties, Telescope.email.templates[template]);
|
2015-12-14 12:43:45 +09:00
|
|
|
}
|
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-04-01 12:38:50 +09:00
|
|
|
Telescope.email.buildTemplate = function (htmlContent) {
|
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'),
|
|
|
|
siteName: Telescope.settings.get('title'),
|
|
|
|
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'),
|
|
|
|
logoWidth: Telescope.settings.get('logoWidth')
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-04-01 12:38:50 +09:00
|
|
|
var emailHTML = Telescope.email.getTemplate("wrapper")(emailProperties);
|
2015-04-22 07:50:11 +09:00
|
|
|
|
|
|
|
var inlinedHTML = juice(emailHTML);
|
|
|
|
|
|
|
|
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-04-01 12:38:50 +09:00
|
|
|
Telescope.email.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-04-01 12:38:50 +09:00
|
|
|
Telescope.email.buildAndSend = function (to, subject, template, properties) {
|
|
|
|
var html = Telescope.email.buildTemplate(Telescope.email.getTemplate(template)(properties));
|
|
|
|
return Telescope.email.send (to, subject, html);
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-22 07:50:11 +09:00
|
|
|
|
2016-04-13 11:39:01 +09:00
|
|
|
Telescope.email.buildAndSendHTML = function (to, subject, html) {
|
|
|
|
return Telescope.email.send (to, subject, Telescope.email.buildTemplate(html));
|
|
|
|
};
|
|
|
|
|
2015-04-22 07:50:11 +09:00
|
|
|
Meteor.methods({
|
2016-04-13 12:34:41 +09:00
|
|
|
testEmail: function (emailName) {
|
|
|
|
|
|
|
|
const email = Telescope.email.emails[emailName];
|
|
|
|
|
2015-04-27 17:14:07 +09:00
|
|
|
if(Users.is.adminById(this.userId)){
|
2016-04-13 12:34:41 +09:00
|
|
|
|
|
|
|
console.log("// testing email ["+emailName+"]");
|
|
|
|
const item = email.getTestObject();
|
|
|
|
const subject = "[Test] " + email.subject(email.getProperties(item));
|
|
|
|
let html;
|
|
|
|
|
|
|
|
// if email has a custom way of generating test HTML, use it
|
|
|
|
if (typeof email.getTestHTML !== "undefined") {
|
|
|
|
|
|
|
|
html = email.getTestHTML.bind(email)();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// else get test object (sample post, comment, user, etc.)
|
|
|
|
const testObject = email.getTestObject();
|
|
|
|
// get test object's email properties
|
|
|
|
const properties = email.getProperties(testObject);
|
|
|
|
// then apply email template to properties, and wrap it with buildTemplate
|
|
|
|
html = Telescope.email.buildTemplate(Telescope.email.getTemplate(email.template)(properties));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Telescope.email.buildAndSendHTML(Telescope.settings.get('defaultEmail'), subject, html);
|
|
|
|
|
|
|
|
return subject;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
throw new Meteor.Error("must_be_admin", "You must be an admin to send test emails");
|
2015-04-22 07:50:11 +09:00
|
|
|
}
|
|
|
|
}
|
2016-04-01 12:38:50 +09:00
|
|
|
});
|