]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mail] Add support for e-mail headers (#11307)
authorAndrew Fiddian-Green <software@whitebear.ch>
Mon, 27 Sep 2021 08:38:13 +0000 (09:38 +0100)
committerGitHub <noreply@github.com>
Mon, 27 Sep 2021 08:38:13 +0000 (10:38 +0200)
Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
bundles/org.openhab.binding.mail/README.md
bundles/org.openhab.binding.mail/src/main/java/org/openhab/binding/mail/internal/MailBuilder.java
bundles/org.openhab.binding.mail/src/main/java/org/openhab/binding/mail/internal/action/SendMailActions.java
bundles/org.openhab.binding.mail/src/main/resources/OH-INF/i18n/mail.properties
bundles/org.openhab.binding.mail/src/test/java/org/openhab/binding/mail/MailBuilderTest.java

index ad71caf7d854ad58aeadb3c63fd3b1d598e5590d..0f4940bcd42d9d9ed02ea9c463b8007ebfd50b42 100644 (file)
@@ -127,3 +127,23 @@ val List<String> attachmentUrlList = newArrayList(
 val mailActions = getActions("mail","mail:smtp:sampleserver")
 mailActions.sendHtmlMail("mail@example.com", "Test subject", "<h1>Header</h1>This is the mail content.", attachmentUrlList)
 ```
+
+## Mail Headers
+
+The binding allows one to add custom e-mail headers to messages that it sends.
+For example if you want e-mails sent by this binding to be grouped into a "threaded view" in your email client, you must provide an e-mail "Reference" header, which acts as the key for grouping messages together.
+Headers can be added inside a rule by calling the `mailActions.addHeader()` method before calling the respective `mailActions.sendMail()` method.
+See the example below.
+
+```
+rule "Send Mail with a 'Reference' header; for threaded view in e-mail client"
+when
+    ...
+then
+    val mailActions = getActions("mail","mail:smtp:sampleserver")
+    mailActions.addHeader("Reference", "<unique-thread-identifier>")
+    mailActions.sendMail("mail@example.com", "Test subject", "Test message text")
+end
+```
+
+Note: in the case of the "Reference" header, the `<unique-thread-identifier>` has to be an ASCII string enclosed in angle brackets.
index 5edf00f0460ac47538778a9745bcd33ff6a5e3c7..a2df6082a3587199f648d6bbdc8a3348ae4c8e1c 100644 (file)
@@ -17,7 +17,9 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.activation.FileDataSource;
 import javax.mail.internet.AddressException;
@@ -47,6 +49,7 @@ public class MailBuilder {
     private String subject = "(no subject)";
     private String text = "";
     private String html = "";
+    private Map<String, String> headers = new HashMap<>();
 
     /**
      * Create a new MailBuilder
@@ -137,6 +140,11 @@ public class MailBuilder {
         return this;
     }
 
+    public MailBuilder withHeader(String name, String value) {
+        headers.put(name, value);
+        return this;
+    }
+
     /**
      * Build the Mail
      *
@@ -198,6 +206,8 @@ public class MailBuilder {
             mail.setFrom(sender);
         }
 
+        headers.forEach((name, value) -> mail.addHeader(name, value));
+
         return mail;
     }
 }
index 8fa5201649daed323db3cfd1a3c8b41e8f39c508..3564e06f898b3a3c7128eeff1285747bb5f05b21 100644 (file)
@@ -14,7 +14,9 @@ package org.openhab.binding.mail.internal.action;
 
 import java.net.MalformedURLException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.mail.internet.AddressException;
 
@@ -44,6 +46,7 @@ public class SendMailActions implements ThingActions {
     private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
 
     private @Nullable SMTPHandler handler;
+    private Map<String, String> headers = new HashMap<>();
 
     @RuleAction(label = "@text/sendMessageActionLabel", description = "@text/sendMessageActionDescription")
     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
@@ -90,6 +93,8 @@ public class SendMailActions implements ThingActions {
                 }
             }
 
+            headers.forEach((name, value) -> builder.withHeader(name, value));
+
             final SMTPHandler handler = this.handler;
             if (handler == null) {
                 logger.info("Handler is null, cannot send mail.");
@@ -167,6 +172,8 @@ public class SendMailActions implements ThingActions {
                 }
             }
 
+            headers.forEach((name, value) -> builder.withHeader(name, value));
+
             final SMTPHandler handler = this.handler;
             if (handler == null) {
                 logger.warn("Handler is null, cannot send mail.");
@@ -210,4 +217,22 @@ public class SendMailActions implements ThingActions {
     public @Nullable ThingHandler getThingHandler() {
         return handler;
     }
+
+    @RuleAction(label = "@text/addHeaderActionLabel", description = "@text/addHeaderActionDescription")
+    public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean addHeader(
+            @ActionInput(name = "name") @Nullable String name, @ActionInput(name = "value") @Nullable String value) {
+        if (name != null && !name.isEmpty()) {
+            if (value != null && !value.isEmpty()) {
+                headers.put(name, value);
+            } else {
+                headers.remove(name);
+            }
+            return true;
+        }
+        return false;
+    }
+
+    public static boolean addHeader(ThingActions actions, @Nullable String name, @Nullable String value) {
+        return ((SendMailActions) actions).addHeader(name, value);
+    }
 }
index eb66ef566d0d13d65ec304f652f3cd3994236bb1..c7a481fb2ffae8f5e5f9737dbfe6bf5ee83892e6 100644 (file)
@@ -16,3 +16,6 @@ sendHTMLAttachmentMessageActionDescription = Sends a HTML mail with an URL attac
 
 sendHTMLAttachmentsMessageActionLabel = send a HTML mail with several attachments
 sendHTMLAttachmentsMessageActionDescription = Sends a HTML mail with several URL attachments.
+
+addHeaderActionLabel = add a mail header
+addHeaderActionDescription = Adds a mail header to the mail message.
index d096d372320bee50a83889414050af543b50d894..70d90d6807c97610daa753ca4d42f6fe625dee6b 100644 (file)
@@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.*;
 
 import java.io.IOException;
 import java.net.MalformedURLException;
+import java.util.Map;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.AddressException;
@@ -41,6 +42,11 @@ public class MailBuilderTest {
     private static final String TEST_STRING = "test";
     private static final String TEST_EMAIL = "foo@bar.zinga";
 
+    private static final String HEADER_1_KEY = "key_one";
+    private static final String HEADER_1_VAL = "value_one";
+    private static final String HEADER_2_KEY = "key_two";
+    private static final String HEADER_2_VAL = "value_two";
+
     @Test
     public void illegalToAddressThrowsException() {
         assertThrows(AddressException.class, () -> new MailBuilder("foo bar.zinga"));
@@ -91,4 +97,16 @@ public class MailBuilderTest {
         assertEquals(TEST_EMAIL, builder.build().getToAddresses().get(0).getAddress());
         assertEquals(2, builder.withRecipients(TEST_EMAIL).build().getToAddresses().size());
     }
+
+    @Test
+    public void withHeaders() throws EmailException, MessagingException, IOException {
+        MailBuilder builder = new MailBuilder(TEST_EMAIL);
+        Email mail = builder.withHeader(HEADER_1_KEY, HEADER_1_VAL).withHeader(HEADER_2_KEY, HEADER_2_VAL).build();
+
+        Map<String, String> headers = mail.getHeaders();
+
+        assertEquals(2, headers.size());
+        assertEquals(HEADER_2_VAL, headers.get(HEADER_2_KEY));
+        assertEquals(HEADER_1_VAL, headers.get(HEADER_1_KEY));
+    }
 }