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>EMAIL</strong> {t('emailAddress')} (<em>{t('required')}</em>)</li>
<li><strong>SEND_CONFIGURATION_ID</strong> {t('sendConfigurationId')}</li> <li><strong>SEND_CONFIGURATION_ID</strong> {t('sendConfigurationId')}</li>
<li><strong>SUBJECT</strong> {t('subject')}</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> </ul>
<p> <p>

View file

@ -965,7 +965,8 @@
"thePasswordMustContainAtLeastOne-1": "The password must contain at least one uppercase letter", "thePasswordMustContainAtLeastOne-1": "The password must contain at least one uppercase letter",
"thePasswordMustContainAtLeastOneNumber": "The password must contain at least one number", "thePasswordMustContainAtLeastOneNumber": "The password must contain at least one number",
"thePasswordMustContainAtLeastOneSpecial": "The password must contain at least one special character", "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", "sendTransactionalEmail": "Send transactional email",
"sendSingleEmailByTemplateId": "Send single template by :templateId", "sendSingleEmailByTemplateId": "Send single template by :templateId",
"sendConfigurationId": "ID of configuration used to create mailer instance" "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", "thePasswordMustContainAtLeastOne-1": "The password must contain at least one uppercase letter",
"thePasswordMustContainAtLeastOneNumber": "The password must contain at least one number", "thePasswordMustContainAtLeastOneNumber": "The password must contain at least one number",
"thePasswordMustContainAtLeastOneSpecial": "The password must contain at least one special character", "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", "sendTransactionalEmail": "Send transactional email",
"sendSingleEmailByTemplateId": "Send single template by :templateId", "sendSingleEmailByTemplateId": "Send single template by :templateId",
"sendConfigurationId": "ID of configuration used to create mailer instance" "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([ const [mailer, template] = await Promise.all([
mailers.getOrCreateMailer(options.sendConfigurationId), mailers.getOrCreateMailer(options.sendConfigurationId),
templates.getById( templates.getById(options.context, this.templateId, false)
options.context,
this.templateId,
false
)
]); ]);
const html = this._substituteVariables( const html = this._substituteVariables(
template.html, template.html,
options.variables options.variables
); );
const subject = this._substituteVariables(
options.subject || template.description || template.name,
options.variables
);
return mailer.sendTransactionalMail( return mailer.sendTransactionalMail(
{ {
to: options.email, to: options.email,
subject: options.subject subject
}, },
{ {
html: { template: html }, html: { template: html },
data: options.data,
locale: options.locale locale: options.locale
} }
); );
@ -64,13 +65,14 @@ class TemplateSender {
} }
_substituteVariables(html, variables) { _substituteVariables(html, variables) {
if (!variables) return html; if (!variables) {
return Object.keys(variables).reduce((res, key) => { return html;
return res.replace( }
new RegExp(`\\[${key}\\]`, 'gmi'), return Object.keys(variables).reduce(
variables[key] (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({ const info = await templateSender.send({
context: req.context, context: req.context,
data: input.DATA,
email: input.EMAIL, email: input.EMAIL,
locale: req.locale, locale: req.locale,
sendConfigurationId: input.SEND_CONFIGURATION_ID, sendConfigurationId: input.SEND_CONFIGURATION_ID,