-
Notifications
You must be signed in to change notification settings - Fork 217
Email template params interpolation #1345
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
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); | ||
|
Contributor
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
Author
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.
Contributor
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.
Author
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?
Contributor
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
Contributor
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("[,;]"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -407,16 +407,38 @@ public void testEmailTemplate() throws Exception { | |
|
|
||
| Map<String, Serializable> params = new HashMap<>(); | ||
| params.put("userName", "Bob"); | ||
| params.put("greeting", "Hello"); | ||
| params.put("dateParam", new SimpleDateFormat("dd/MM/yyyy").parse("01/05/2013")); | ||
|
|
||
| EmailInfo info = new EmailInfo("[email protected]", "Test", null, templateFileName, params); | ||
| EmailInfo info = new EmailInfo("[email protected]", "${greeting} Test", null, templateFileName, params); | ||
| emailer.sendEmailAsync(info); | ||
| emailer.processQueuedEmails(); | ||
|
|
||
| String body = getBody(testMailSender.fetchSentEmail()); | ||
| MimeMessage sent = testMailSender.fetchSentEmail(); | ||
| String body = getBody(sent); | ||
| assertEquals("Greetings, Bob! 01-05-2013", body.trim()); | ||
|
|
||
| String caption = getCaption(sent); | ||
| assertEquals("Hello Test", caption.trim()); | ||
|
|
||
| info = new EmailInfo("[email protected]", "${greeting} Test", null, null, params); | ||
| info.setBody("Greetings, ${userName}!"); | ||
| emailer.sendEmailAsync(info); | ||
| emailer.processQueuedEmails(); | ||
| sent = testMailSender.fetchSentEmail(); | ||
| assertEquals("Greetings, Bob!", getBody(sent).trim()); | ||
|
|
||
| info = new EmailInfo("[email protected]", "${greeting} Test", null, null); | ||
| info.setBody("Greetings, ${userName}!"); | ||
| emailer.sendEmailAsync(info); | ||
| emailer.processQueuedEmails(); | ||
| sent = testMailSender.fetchSentEmail(); | ||
|
|
||
| assertEquals("Greetings, ${userName}!", getBody(sent).trim()); | ||
| assertEquals("${greeting} Test", getCaption(sent).trim()); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| public void testTextAttachment() throws Exception { | ||
| doTestTextAttachment(false); | ||
|
|
@@ -609,6 +631,10 @@ private String getBody(MimeMessage msg) throws Exception { | |
| return (String) textPart.getContent(); | ||
| } | ||
|
|
||
| private String getCaption(MimeMessage msg) throws Exception { | ||
| return msg.getSubject(); | ||
| } | ||
|
|
||
| private String getBodyContentType(MimeMessage msg) throws Exception { | ||
| MimeBodyPart textPart = getTextPart(msg); | ||
| return textPart.getContentType(); | ||
|
|
||
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