]> git.basschouten.com Git - openhab-addons.git/blob
d08cf07284ccbe4bc7b8dbff175a8d112b864f2c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mail.internal.action;
14
15 import java.net.MalformedURLException;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import javax.mail.internet.AddressException;
22
23 import org.apache.commons.mail.EmailException;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.mail.internal.MailBuilder;
27 import org.openhab.binding.mail.internal.SMTPHandler;
28 import org.openhab.core.automation.annotation.ActionInput;
29 import org.openhab.core.automation.annotation.ActionOutput;
30 import org.openhab.core.automation.annotation.RuleAction;
31 import org.openhab.core.thing.binding.ThingActions;
32 import org.openhab.core.thing.binding.ThingActionsScope;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link SendMailActions} class defines rule actions for sending mail
39  *
40  * @author Jan N. Klug - Initial contribution
41  */
42 @ThingActionsScope(name = "mail")
43 @NonNullByDefault
44 public class SendMailActions implements ThingActions {
45
46     private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
47
48     private @Nullable SMTPHandler handler;
49     private Map<String, String> headers = new HashMap<>();
50
51     @RuleAction(label = "@text/sendMessageActionLabel", description = "@text/sendMessageActionDescription")
52     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
53             @ActionInput(name = "recipient") @Nullable String recipient,
54             @ActionInput(name = "subject") @Nullable String subject,
55             @ActionInput(name = "text") @Nullable String text) {
56         return sendMailWithAttachments(recipient, subject, text, List.of());
57     }
58
59     @RuleAction(label = "@text/sendAttachmentMessageActionLabel", description = "@text/sendAttachmentMessageActionDescription")
60     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMailWithAttachment(
61             @ActionInput(name = "recipient") @Nullable String recipient,
62             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
63             @ActionInput(name = "url") @Nullable String urlString) {
64         List<String> urlList = new ArrayList<>();
65         if (urlString != null) {
66             urlList.add(urlString);
67         }
68         return sendMailWithAttachments(recipient, subject, text, urlList);
69     }
70
71     @RuleAction(label = "@text/sendAttachmentsMessageActionLabel", description = "@text/sendAttachmentsMessageActionDescription")
72     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMailWithAttachments(
73             @ActionInput(name = "recipient") @Nullable String recipient,
74             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
75             @ActionInput(name = "urlList") @Nullable List<String> urlStringList) {
76         if (recipient == null) {
77             logger.warn("Cannot send mail as recipient is missing.");
78             return false;
79         }
80
81         try {
82             MailBuilder builder = new MailBuilder(recipient);
83
84             if (subject != null && !subject.isEmpty()) {
85                 builder.withSubject(subject);
86             }
87             if (text != null && !text.isEmpty()) {
88                 builder.withText(text);
89             }
90             if (urlStringList != null) {
91                 for (String urlString : urlStringList) {
92                     builder.withURLAttachment(urlString);
93                 }
94             }
95
96             headers.forEach((name, value) -> builder.withHeader(name, value));
97
98             final SMTPHandler handler = this.handler;
99             if (handler == null) {
100                 logger.info("Handler is null, cannot send mail.");
101                 return false;
102             } else {
103                 return handler.sendMail(builder.build());
104             }
105         } catch (AddressException | MalformedURLException | EmailException e) {
106             logger.warn("Could not send mail: {}", e.getMessage());
107             return false;
108         }
109     }
110
111     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
112             @Nullable String text) {
113         return SendMailActions.sendMail(actions, recipient, subject, text, List.of());
114     }
115
116     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
117             @Nullable String text, @Nullable String urlString) {
118         List<String> urlList = new ArrayList<>();
119         if (urlString != null) {
120             urlList.add(urlString);
121         }
122         return SendMailActions.sendMail(actions, recipient, subject, text, urlList);
123     }
124
125     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
126             @Nullable String text, @Nullable List<String> urlStringList) {
127         return ((SendMailActions) actions).sendMailWithAttachments(recipient, subject, text, urlStringList);
128     }
129
130     @RuleAction(label = "@text/sendHTMLMessageActionLabel", description = "@text/sendHTMLMessageActionDescription")
131     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
132             @ActionInput(name = "recipient") @Nullable String recipient,
133             @ActionInput(name = "subject") @Nullable String subject,
134             @ActionInput(name = "html") @Nullable String html) {
135         return sendHtmlMailWithAttachments(recipient, subject, html, List.of());
136     }
137
138     @RuleAction(label = "@text/sendHTMLAttachmentMessageActionLabel", description = "@text/sendHTMLAttachmentMessageActionDescription")
139     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachment(
140             @ActionInput(name = "recipient") @Nullable String recipient,
141             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
142             @ActionInput(name = "url") @Nullable String urlString) {
143         List<String> urlList = new ArrayList<>();
144         if (urlString != null) {
145             urlList.add(urlString);
146         }
147         return sendHtmlMailWithAttachments(recipient, subject, html, urlList);
148     }
149
150     @RuleAction(label = "@text/sendHTMLAttachmentsMessageActionLabel", description = "@text/sendHTMLAttachmentsMessageActionDescription")
151     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachments(
152             @ActionInput(name = "recipient") @Nullable String recipient,
153             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
154             @ActionInput(name = "urlList") @Nullable List<String> urlStringList) {
155         if (recipient == null) {
156             logger.warn("Cannot send mail as recipient is missing.");
157             return false;
158         }
159
160         try {
161             MailBuilder builder = new MailBuilder(recipient);
162
163             if (subject != null && !subject.isEmpty()) {
164                 builder.withSubject(subject);
165             }
166             if (html != null && !html.isEmpty()) {
167                 builder.withHtml(html);
168             }
169             if (urlStringList != null) {
170                 for (String urlString : urlStringList) {
171                     builder.withURLAttachment(urlString);
172                 }
173             }
174
175             headers.forEach((name, value) -> builder.withHeader(name, value));
176
177             final SMTPHandler handler = this.handler;
178             if (handler == null) {
179                 logger.warn("Handler is null, cannot send mail.");
180                 return false;
181             } else {
182                 return handler.sendMail(builder.build());
183             }
184         } catch (AddressException | MalformedURLException | EmailException e) {
185             logger.warn("Could not send mail: {}", e.getMessage());
186             return false;
187         }
188     }
189
190     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
191             @Nullable String html) {
192         return SendMailActions.sendHtmlMail(actions, recipient, subject, html, List.of());
193     }
194
195     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
196             @Nullable String html, @Nullable String urlString) {
197         List<String> urlList = new ArrayList<>();
198         if (urlString != null) {
199             urlList.add(urlString);
200         }
201         return SendMailActions.sendHtmlMail(actions, recipient, subject, html, urlList);
202     }
203
204     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
205             @Nullable String html, @Nullable List<String> urlStringList) {
206         return ((SendMailActions) actions).sendHtmlMailWithAttachments(recipient, subject, html, urlStringList);
207     }
208
209     @Override
210     public void setThingHandler(@Nullable ThingHandler handler) {
211         if (handler instanceof SMTPHandler) {
212             this.handler = (SMTPHandler) handler;
213         }
214     }
215
216     @Override
217     public @Nullable ThingHandler getThingHandler() {
218         return handler;
219     }
220
221     @RuleAction(label = "@text/addHeaderActionLabel", description = "@text/addHeaderActionDescription")
222     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean addHeader(
223             @ActionInput(name = "name") @Nullable String name, @ActionInput(name = "value") @Nullable String value) {
224         if (name != null && !name.isEmpty()) {
225             if (value != null && !value.isEmpty()) {
226                 headers.put(name, value);
227             } else {
228                 headers.remove(name);
229             }
230             return true;
231         }
232         return false;
233     }
234
235     public static boolean addHeader(ThingActions actions, @Nullable String name, @Nullable String value) {
236         return ((SendMailActions) actions).addHeader(name, value);
237     }
238 }