diff --git a/modules/core/src/com/haulmont/cuba/core/app/Emailer.java b/modules/core/src/com/haulmont/cuba/core/app/Emailer.java index f93f44a9f9..080e85d489 100644 --- a/modules/core/src/com/haulmont/cuba/core/app/Emailer.java +++ b/modules/core/src/com/haulmont/cuba/core/app/Emailer.java @@ -128,6 +128,7 @@ public List 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 params = info.getTemplateParameters() == null - ? Collections.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); + 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 splitEmail(EmailInfo info, @Nullable Integer attemptsCount, @Nullable Date deadline) { List sendingMessageList = new ArrayList<>(); String[] splitAddresses = info.getAddresses().split("[,;]"); diff --git a/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java b/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java index 99788b9de2..f5948b812f 100644 --- a/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java +++ b/modules/core/test/com/haulmont/cuba/core/app/EmailerTest.java @@ -407,16 +407,38 @@ public void testEmailTemplate() throws Exception { Map 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("bob@example.com", "Test", null, templateFileName, params); + EmailInfo info = new EmailInfo("bob@example.com", "${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("bob@example.com", "${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("bob@example.com", "${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();