add support for transactional html/text emails

- Add support for welcome email on user creation (both OAuth and password users)
- Add support for html/text templating on verify email.
- Add support for html/text templating on reset password.
- Add handlers for converting Handlebars templates to HTML and text (template agnostic).
- Add helper function sendEmail() for compiling and sending templates via Email.send() (SMTP).
This commit is contained in:
cleverbeagle 2017-09-07 17:23:08 -05:00
parent aa14a26bba
commit 379f7dcd76
17 changed files with 2234 additions and 804 deletions

View file

@ -0,0 +1,11 @@
import handlebars from 'handlebars';
import juice from 'juice';
export default (handlebarsMarkup, context, options) => {
if (handlebarsMarkup && context) {
const template = handlebars.compile(handlebarsMarkup);
return options && !options.inlineCss ? template(context) : juice(template(context)); // Use juice to inline CSS <style></style> styles from <head> unless disabled.
}
throw new Error('Please pass Handlebars markup to compile and a context object with data mapping to the Handlebars expressions used in your template.');
};

View file

@ -0,0 +1,10 @@
import handlebars from 'handlebars';
export default (handlebarsMarkup, context) => {
if (handlebarsMarkup && context) {
const template = handlebars.compile(handlebarsMarkup);
return template(context);
}
throw new Error('Please pass Handlebars markup to compile and a context object with data mapping to the Handlebars expressions used in your template.');
};

View file

@ -0,0 +1,26 @@
import { Email } from 'meteor/email';
import getPrivateFile from './get-private-file';
import templateToText from './handlebars-email-to-text';
import templateToHTML from './handlebars-email-to-html';
const sendEmail = (options, { resolve, reject }) => {
try {
Meteor.defer(() => Email.send(options));
if (callback) resolve();
} catch (exception) {
reject(exception);
}
};
export default ({ text, html, template, templateVars, ...rest }, callback) => {
if (text || html || template) {
return new Promise((resolve, reject) => {
sendEmail({
...rest,
text: template ? templateToText(getPrivateFile(`email-templates/${template}.txt`), (templateVars || {})) : text,
html: template ? templateToHTML(getPrivateFile(`email-templates/${template}.html`), (templateVars || {})) : html,
}, { resolve, reject });
});
}
throw new Error('Please pass an HTML string, text, or template name to compile for your message\'s body.');
};