Commit 06a9d314 authored by wu-lee's avatar wu-lee

serve.js,email.js - also send an ack email to registrant

parent b0f04e75
......@@ -24,16 +24,22 @@ module.exports = {
tls: true,
},
registration: {
// REQUIRED field!
// REQUIRED fields!
// Where replies to notifications/acknowledgements go
from: "Registrations <registrations@example.com>",
// REQUIRED field!
// Where notifications go (acknowledgements go to the
// registrant).
to: "Registrations <registrations@example.com>",
subject: "Someone has registered!",
// The subject line for notifications
notification: "Someone has registered!",
// The subject line for acknowledgements
acknowledgement: "Thanks for registering!",
},
template: {
// content: 'inline {{ handlebars }} template here'
// file: './handlebars-template.hbs',
// This config file included in template context under 'config' key.
templates: {
// notification: 'path/to/notification-template.hbs',
// acknowledgement: 'path/to/acknowledgement-template.hbs',
// Theme config file included in template context under 'config' key.
// Also, request under 'request'
},
},
......
......@@ -4,6 +4,14 @@ const toText = require('html2plaintext');
module.exports = (serverConfig = {}) => {
/** Sends a notification email.
*
* @param config - the emailjs config
* @param html - the HTML message (will have a text version added)
* @param json - optional JSON attachment (the registration data)
* @returns a promise which can be used to obtain the success
* or failure of this (potentially slow) operation.
*/
async function send(config, html, json) {
if (!config.from)
throw new Error("must send with a from: parameter");
......@@ -12,15 +20,17 @@ module.exports = (serverConfig = {}) => {
var html = String(html);
const server = email.server.connect(serverConfig);
const attachments = [{data: html, alternative: true}];
if (json)
attachments.push({
data: JSON.stringify(json),
name: 'form-data.json',
type: 'application/json'
});
const message = {
...config,
text: toText(html),
attachment: [
{data: html, alternative: true},
{data: JSON.stringify(json),
name: 'form-data.json',
type: 'application/json'},
],
attachment: attachments
};
// Send the message and await completion. Return
......
......@@ -40,34 +40,47 @@ var fileOptions = {
}
};
// Define a function which returns the notification template
const defaultTemplate = (context) => `<pre>${JSON.stringify(context.request.body, null, 2)}</pre>`;
var getTemplate = () => defaultTemplate;
if (config.email.template) {
var content = config.email.template.content;
if (content) {
maillog.info('loading email notification template from config');
const template = handlebars.compile(content);
getTemplate = () => template;
}
else {
const file = config.email.template.file;
maillog.info('email notification template will be loaded from the file: '+file);
getTemplate = () => {
if (file) {
if (fs.existsSync(file)) {
maillog.info('loading email notification template from file: '+file);
content = fs.readFileSync(file, 'utf8');
return handlebars.compile(content);
}
else {
maillog.warn('no such email notification template file: '+file);
return defaultTemplate;
}
}
};
// Define functions which returns the email templates. These are
// re-loaded on each send so that they can be defined by files in the
// source (with inline handlebars markup). (These templates can be
// excluded from appearing in the output wiki by various means.) The
// one required field in submitted data is an 'email' field with a
// recipient address. All fields, plus parts of the config, are
// supplied to the templates.
function defineTemplateLoader(file, name, defaultTemplate) {
if (!file) {
maillog.warn(`no configured ${name} template file, using default`);
return defaultTemplate;
}
maillog.info(`${name} will be loaded from the file: ${file}`);
return (context) => {
if (fs.existsSync(file)) {
maillog.info(`loading ${name} from file: ${file}`);
const content = fs.readFileSync(file, 'utf8');
return handlebars.compile(content)(context);
}
else {
maillog.warn(`using default as no ${name} found at ${file}`);
return defaultTemplate(context);
}
};
}
var notificationTemplate, acknowledgementTemplate;
if (config.email && config.email.templates) {
notificationTemplate = config.email.templates.notification;
acknowledgementTemplate = config.email.templates.acknowledgement;
}
const notificationContent = defineTemplateLoader(
notificationTemplate,
'email notification template',
(context) =>
`<pre>${JSON.stringify(context.request.body, null, 2)}</pre>`
);
const acknowledgementContent = defineTemplateLoader(
acknowledgementTemplate,
'email acknowledgement template',
(context) => 'Thanks! We shall be in touch.'
);
async function build() {
buildlog.info('starting build...')
......@@ -147,19 +160,52 @@ app.post('/registration', (req, res) => {
submitlog.info("error emailing registration notification, ",
err);
}
submitlog.info('emailing registration notification: ', req.body);
// Send notification of registration to admins
const sendConfig = config.email.registration || {};
const templateContext = {
config: {theme: config.theme},
request: req
};
submitlog.info(`emailing registration notification to ${sendConfig.to}`, req.body);
const notificationConfig = {
from: sendConfig.from,
to: sendConfig.to,
subject: sendConfig.notification,
};
try {
const html = getTemplate()({config, request: req});
send(sendConfig,
const html = notificationContent(templateContext);
send(notificationConfig,
html,
req.body)
.then(ok, error);
}
catch(e) {
maillog.error('failed to send notification', e);
maillog.error(`failed to send notification of registration to ${notificationConfig.to}: `, e);
}
if (req.body.email) {
// Send acknowledgement email to registrant
const acknowledgementConfig = {
from: sendConfig.from,
to: req.body.email,
subject: sendConfig.acknowledgement,
};
try {
const html = acknowledgementContent(templateContext);
submitlog.info(`emailing registration acknowledgement to ${acknowledgementConfig.to}: `);
send(acknowledgementConfig,
html)
.then(ok, error);
}
catch(e) {
maillog.error(`failed to send registration acknowledgement to ${acknowledgementConfig.to} `, e);
}
}
else {
maillog.error(`cannot send registration acknowledgement, no email set in registration`);
}
res.redirect('/home');
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment