diff --git a/client/src/account/API.js b/client/src/account/API.js index 56af2106..724433ec 100644 --- a/client/src/account/API.js +++ b/client/src/account/API.js @@ -389,7 +389,8 @@ export default class API extends Component {
  • EMAIL – {t('emailAddress')} ({t('required')})
  • SEND_CONFIGURATION_ID – {t('sendConfigurationId')}
  • SUBJECT – {t('subject')}
  • -
  • VARIABLES – {t('templateVariables')}: {"{"} FOO: bar {"}"}
  • +
  • DATA – {t('templateData')}: {'{'} "any": ["type", {'{'}"of": "data"{'}'}] {'}'}
  • +
  • VARIABLES – {t('templateVariables')}: {'{'} "FOO": "bar" {'}'}
  • diff --git a/locales/en-US/common.json b/locales/en-US/common.json index 20d5b6d9..7dcce754 100644 --- a/locales/en-US/common.json +++ b/locales/en-US/common.json @@ -965,7 +965,8 @@ "thePasswordMustContainAtLeastOne-1": "The password must contain at least one uppercase letter", "thePasswordMustContainAtLeastOneNumber": "The password must contain at least one number", "thePasswordMustContainAtLeastOneSpecial": "The password must contain at least one special character", - "templateVariables": "Map of template variables to replace", + "templateData": "Data passed to template when compiling with Handlebars", + "templateVariables": "Map of template/subject variables to replace", "sendTransactionalEmail": "Send transactional email", "sendSingleEmailByTemplateId": "Send single template by :templateId", "sendConfigurationId": "ID of configuration used to create mailer instance" diff --git a/locales/es-ES/common.json b/locales/es-ES/common.json index 247b23b5..e26f9507 100644 --- a/locales/es-ES/common.json +++ b/locales/es-ES/common.json @@ -965,7 +965,8 @@ "thePasswordMustContainAtLeastOne-1": "The password must contain at least one uppercase letter", "thePasswordMustContainAtLeastOneNumber": "The password must contain at least one number", "thePasswordMustContainAtLeastOneSpecial": "The password must contain at least one special character", - "templateVariables": "Map of template variables to replace", + "templateData": "Data passed to template when compiling with Handlebars", + "templateVariables": "Map of template/subject variables to replace", "sendTransactionalEmail": "Send transactional email", "sendSingleEmailByTemplateId": "Send single template by :templateId", "sendConfigurationId": "ID of configuration used to create mailer instance" diff --git a/server/.eslintrc b/server/.eslintrc index 91926fcd..360c05cb 100644 --- a/server/.eslintrc +++ b/server/.eslintrc @@ -1,3 +1,4 @@ { - "extends": "nodemailer" + "extends": "nodemailer", + "parserOptions": { "ecmaVersion": 2017 } } diff --git a/server/lib/template-sender.js b/server/lib/template-sender.js index 5ea4db67..d077f1b7 100644 --- a/server/lib/template-sender.js +++ b/server/lib/template-sender.js @@ -18,24 +18,25 @@ class TemplateSender { const [mailer, template] = await Promise.all([ mailers.getOrCreateMailer(options.sendConfigurationId), - templates.getById( - options.context, - this.templateId, - false - ) + templates.getById(options.context, this.templateId, false) ]); const html = this._substituteVariables( template.html, options.variables ); + const subject = this._substituteVariables( + options.subject || template.description || template.name, + options.variables + ); return mailer.sendTransactionalMail( { to: options.email, - subject: options.subject + subject }, { html: { template: html }, + data: options.data, locale: options.locale } ); @@ -64,13 +65,14 @@ class TemplateSender { } _substituteVariables(html, variables) { - if (!variables) return html; - return Object.keys(variables).reduce((res, key) => { - return res.replace( - new RegExp(`\\[${key}\\]`, 'gmi'), - variables[key] - ); - }, html); + if (!variables) { + return html; + } + return Object.keys(variables).reduce( + (res, key) => + res.replace(new RegExp(`\\[${key}\\]`, 'gmi'), variables[key]), + html + ); } } diff --git a/server/routes/api.js b/server/routes/api.js index 60fa8a62..69a6b10b 100644 --- a/server/routes/api.js +++ b/server/routes/api.js @@ -303,6 +303,7 @@ router.postAsync('/templates/:templateId/send', async (req, res) => { }); const info = await templateSender.send({ context: req.context, + data: input.DATA, email: input.EMAIL, locale: req.locale, sendConfigurationId: input.SEND_CONFIGURATION_ID,