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

@ -1,5 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import getPrivateFile from '../../../modules/server/get-private-file';
import templateToHTML from '../../../modules/server/handlebars-email-to-html';
import templateToText from '../../../modules/server/handlebars-email-to-text';
const name = 'Application Name';
const email = '<support@application.com>';
@ -13,11 +16,21 @@ emailTemplates.verifyEmail = {
subject() {
return `[${name}] Verify Your Email Address`;
},
html(user, url) {
return templateToHTML(getPrivateFile('email-templates/verify-email.html'), {
applicationName: name,
firstName: user.profile.name.first,
verifyUrl: url.replace('#/', '')
});
},
text(user, url) {
const userEmail = user.emails[0].address;
const urlWithoutHash = url.replace('#/', '');
if (Meteor.isDevelopment) console.info(`Verify Email Link: ${urlWithoutHash}`); // eslint-disable-line
return `Hey, ${user.profile.name.first}! Welcome to ${name}.\n\nTo verify your email address (${userEmail}), click the link below:\n\n${urlWithoutHash}\n\nIf you feel something is wrong, please contact our support team: ${email}.`;
return templateToText(getPrivateFile('email-templates/verify-email.txt'), {
applicationName: name,
firstName: user.profile.name.first,
verifyUrl: urlWithoutHash,
});
},
};
@ -25,10 +38,22 @@ emailTemplates.resetPassword = {
subject() {
return `[${name}] Reset Your Password`;
},
html(user, url) {
return templateToHTML(getPrivateFile('email-templates/reset-password.html'), {
firstName: user.profile.name.first,
applicationName: name,
emailAddress: user.emails[0].address,
resetUrl: url.replace('#/', ''),
});
},
text(user, url) {
const userEmail = user.emails[0].address;
const urlWithoutHash = url.replace('#/', '');
if (Meteor.isDevelopment) console.info(`Reset Password Link: ${urlWithoutHash}`); // eslint-disable-line
return `A password reset has been requested for the account related to this address (${userEmail}).\n\nTo reset the password, visit the following link: \n\n${urlWithoutHash}\n\n If you did not request this reset, please ignore this email. If you feel something is wrong, please contact our support team: ${email}.`;
return templateToText(getPrivateFile('email-templates/reset-password.txt'), {
firstName: user.profile.name.first,
applicationName: name,
emailAddress: user.emails[0].address,
resetUrl: urlWithoutHash,
});
},
};

View file

@ -1,7 +1,9 @@
import { Accounts } from 'meteor/accounts-base';
import sendWelcomeEmail from '../../../api/Users/server/send-welcome-email';
Accounts.onCreateUser((options, user) => {
const userToCreate = user;
if (options.profile) userToCreate.profile = options.profile;
sendWelcomeEmail(options, user);
return userToCreate;
});