diff --git a/src/main/java/org/alfresco/repo/action/executer/MailActionExecuter.java b/src/main/java/org/alfresco/repo/action/executer/MailActionExecuter.java index b5b4cf1..157a124 100644 --- a/src/main/java/org/alfresco/repo/action/executer/MailActionExecuter.java +++ b/src/main/java/org/alfresco/repo/action/executer/MailActionExecuter.java @@ -1634,14 +1634,21 @@ public class MailActionExecuter extends ActionExecuterAbstractBase { boolean result = false; - // Note: only simplistic match here - expects = htmlPrefix.length() && trimmedText.substring(0, htmlPrefix.length()).equalsIgnoreCase(htmlPrefix)) { result = true; } + else if (trimmedText.length() >= dtPrefix.length() && + trimmedText.substring(0, dtPrefix.length()).equalsIgnoreCase(dtPrefix)) + { + result = true; + } return result; } diff --git a/src/test/java/org/alfresco/repo/action/executer/AbstractMailActionExecuterTest.java b/src/test/java/org/alfresco/repo/action/executer/AbstractMailActionExecuterTest.java index 469a774..7a02943 100644 --- a/src/test/java/org/alfresco/repo/action/executer/AbstractMailActionExecuterTest.java +++ b/src/test/java/org/alfresco/repo/action/executer/AbstractMailActionExecuterTest.java @@ -246,6 +246,45 @@ public abstract class AbstractMailActionExecuterTest Assert.assertEquals("Bonjour 1 janv. 1970", (String) message.getContent()); } + @Test + public void testHTMLDetection() throws IOException, MessagingException + { + String from = "some.body@example.com"; + Serializable recipients = (Serializable) Arrays.asList(FRENCH_USER.getUsername()); + String subject = ""; + + Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME); + mailAction.setParameterValue(MailActionExecuter.PARAM_TO_MANY, recipients); + + // First with plain text + String text = "This is plain text\nOnly\nBut it mentions HTML and "; + + MimeMessage message = sendMessage(from, subject, null, text, mailAction); + + Assert.assertNotNull(message); + Assert.assertEquals(text, (String) message.getContent()); + Assert.assertEquals("text/plain", // Ignore charset + message.getDataHandler().getContentType().substring(0, 10)); + + // HTML opening tag + text = "HTML emails are great"; + message = sendMessage(from, subject, null, text, mailAction); + + Assert.assertNotNull(message); + Assert.assertEquals(text, (String) message.getContent()); + Assert.assertEquals("text/html", // Ignore charset + message.getDataHandler().getContentType().substring(0, 9)); + + // HTML Doctype + text = "\n\nMore complex HTML"; + message = sendMessage(from, subject, null, text, mailAction); + + Assert.assertNotNull(message); + Assert.assertEquals(text, (String) message.getContent()); + Assert.assertEquals("text/html", // Ignore charset + message.getDataHandler().getContentType().substring(0, 9)); + } + protected MimeMessage sendMessage(String from, Serializable recipients, String subject, String template) { Action mailAction = ACTION_SERVICE.createAction(MailActionExecuter.NAME); @@ -256,13 +295,24 @@ public abstract class AbstractMailActionExecuterTest protected MimeMessage sendMessage(String from, String subject, String template, final Action mailAction) { + return sendMessage(from, subject, template, null, mailAction); + } + protected MimeMessage sendMessage(String from, String subject, String template, String bodyText, final Action mailAction) + { if (from != null) { mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, from); } mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, subject); - mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, template); - mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, getModel()); + if (template != null) + { + mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, template); + mailAction.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, getModel()); + } + else + { + mailAction.setParameterValue(MailActionExecuter.PARAM_TEXT, bodyText); + } RetryingTransactionHelper txHelper = APP_CONTEXT_INIT.getApplicationContext().getBean("retryingTransactionHelper", RetryingTransactionHelper.class);