Transactional mail: added data rendering

This commit is contained in:
Alexey Zinkevych 2019-04-02 14:38:12 +03:00
parent 80279346f3
commit 76b4f8b8c2
6 changed files with 24 additions and 17 deletions

View file

@ -389,7 +389,8 @@ export default class API extends Component {
<li><strong>EMAIL</strong> {t('emailAddress')} (<em>{t('required')}</em>)</li>
<li><strong>SEND_CONFIGURATION_ID</strong> {t('sendConfigurationId')}</li>
<li><strong>SUBJECT</strong> {t('subject')}</li>
<li><strong>VARIABLES</strong> {t('templateVariables')}: <em>{"{"} FOO: bar {"}"}</em></li>
<li><strong>DATA</strong> {t('templateData')}: <em>{'{'} "any": ["type", {'{'}"of": "data"{'}'}] {'}'}</em></li>
<li><strong>VARIABLES</strong> {t('templateVariables')}: <em>{'{'} "FOO": "bar" {'}'}</em></li>
</ul>
<p>

View file

@ -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"

View file

@ -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"

View file

@ -1,3 +1,4 @@
{
"extends": "nodemailer"
"extends": "nodemailer",
"parserOptions": { "ecmaVersion": 2017 }
}

View file

@ -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]
if (!variables) {
return html;
}
return Object.keys(variables).reduce(
(res, key) =>
res.replace(new RegExp(`\\[${key}\\]`, 'gmi'), variables[key]),
html
);
}, html);
}
}

View file

@ -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,