-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Email template params interpolation #1345
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,7 @@ public List<SendingMessage> sendEmailAsync(EmailInfo info, Integer attemptsCount | |
|
||
protected void prepareEmailInfo(EmailInfo emailInfo) { | ||
processBodyTemplate(emailInfo); | ||
processCaptionTemplate(emailInfo); | ||
|
||
if (emailInfo.getFrom() == null) { | ||
String defaultFromAddress = config.getFromAddress(); | ||
|
@@ -138,23 +139,36 @@ protected void prepareEmailInfo(EmailInfo emailInfo) { | |
} | ||
} | ||
|
||
protected void processCaptionTemplate(EmailInfo emailInfo) { | ||
String caption = buildStringWithTemplateParams(emailInfo, emailInfo.getCaption()); | ||
emailInfo.setCaption(caption); | ||
} | ||
|
||
protected void processBodyTemplate(EmailInfo info) { | ||
String templatePath = info.getTemplatePath(); | ||
if (templatePath == null) { | ||
return; | ||
String templateContents; | ||
if (templatePath != null) { | ||
templateContents = resources.getResourceAsString(templatePath); | ||
if (templateContents == null) { | ||
throw new IllegalArgumentException("Could not find template by path: " + templatePath); | ||
} | ||
}else{ | ||
templateContents = info.getBody(); | ||
} | ||
|
||
Map<String, Serializable> params = info.getTemplateParameters() == null | ||
? Collections.<String, Serializable>emptyMap() | ||
: info.getTemplateParameters(); | ||
String templateContents = resources.getResourceAsString(templatePath); | ||
if (templateContents == null) { | ||
throw new IllegalArgumentException("Could not find template by path: " + templatePath); | ||
templateContents = buildStringWithTemplateParams(info, templateContents); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here too. Do not try to call template processor if params are empty, this will break existing code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You still call > TemplateHelper.processTemplate(templateContents, params); FreeMarker will process it even if Map is empty. It is not OK. Body/subject can contain symbols that will be interpreted as FreeMarker commands. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, if not null instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, processBodyTemplate is not performed for body and subject if templatePath is null. Please pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This breaks compatibility with existing code where developers do not pass parameters |
||
info.setBody(templateContents); | ||
} | ||
|
||
private String buildStringWithTemplateParams(EmailInfo info, String templateContents) { | ||
String content = templateContents; | ||
if(info.getTemplateParameters() != null && info.getTemplateParameters().size() > 0) { | ||
content = TemplateHelper.processTemplate(templateContents, info.getTemplateParameters()); | ||
} | ||
String body = TemplateHelper.processTemplate(templateContents, params); | ||
info.setBody(body); | ||
return content; | ||
} | ||
|
||
|
||
protected List<SendingMessage> splitEmail(EmailInfo info, @Nullable Integer attemptsCount, @Nullable Date deadline) { | ||
List<SendingMessage> sendingMessageList = new ArrayList<>(); | ||
String[] splitAddresses = info.getAddresses().split("[,;]"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call can break valid caption if template params are empty in case of caption contains FreeMarker reserved symbols.
Probably we should add test for this case