]> git.basschouten.com Git - openhab-addons.git/blob
54b74704bd6d09244009993571bc98d588926f18
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.ServiceScope;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link SendMailActions} class defines rule actions for sending mail
41  *
42  * @author Jan N. Klug - Initial contribution
43  */
44 @Component(scope = ServiceScope.PROTOTYPE, service = SendMailActions.class)
45 @ThingActionsScope(name = "mail")
46 @NonNullByDefault
47 public class SendMailActions implements ThingActions {
48
49     private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
50
51     private @Nullable SMTPHandler handler;
52     private Map<String, String> headers = new HashMap<>();
53
54     @RuleAction(label = "@text/sendMessageActionLabel", description = "@text/sendMessageActionDescription")
55     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
56             @ActionInput(name = "recipient") @Nullable String recipient,
57             @ActionInput(name = "subject") @Nullable String subject,
58             @ActionInput(name = "text") @Nullable String text) {
59         return sendMailWithAttachments(recipient, subject, text, List.of());
60     }
61
62     @RuleAction(label = "@text/sendAttachmentMessageActionLabel", description = "@text/sendAttachmentMessageActionDescription")
63     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMailWithAttachment(
64             @ActionInput(name = "recipient") @Nullable String recipient,
65             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
66             @ActionInput(name = "url") @Nullable String url) {
67         List<String> urlList = new ArrayList<>();
68         if (url != null) {
69             urlList.add(url);
70         }
71         return sendMailWithAttachments(recipient, subject, text, urlList);
72     }
73
74     @RuleAction(label = "@text/sendAttachmentsMessageActionLabel", description = "@text/sendAttachmentsMessageActionDescription")
75     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMailWithAttachments(
76             @ActionInput(name = "recipient") @Nullable String recipient,
77             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
78             @ActionInput(name = "urlList") @Nullable List<String> urlList) {
79         if (recipient == null) {
80             logger.warn("Cannot send mail as recipient is missing.");
81             return false;
82         }
83
84         try {
85             MailBuilder builder = new MailBuilder(recipient);
86
87             if (subject != null && !subject.isEmpty()) {
88                 builder.withSubject(subject);
89             }
90             if (text != null && !text.isEmpty()) {
91                 builder.withText(text);
92             }
93             if (urlList != null) {
94                 for (String urlString : urlList) {
95                     builder.withURLAttachment(urlString);
96                 }
97             }
98
99             headers.forEach((name, value) -> builder.withHeader(name, value));
100
101             final SMTPHandler handler = this.handler;
102             if (handler == null) {
103                 logger.info("Handler is null, cannot send mail.");
104                 return false;
105             } else {
106                 return handler.sendMail(builder.build());
107             }
108         } catch (AddressException | MalformedURLException | EmailException e) {
109             logger.warn("Could not send mail: {}", e.getMessage());
110             return false;
111         }
112     }
113
114     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
115             @Nullable String text) {
116         return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, List.of());
117     }
118
119     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
120             @Nullable String text, @Nullable String url) {
121         return SendMailActions.sendMailWithAttachment(actions, recipient, subject, text, url);
122     }
123
124     public static boolean sendMailWithAttachment(ThingActions actions, @Nullable String recipient,
125             @Nullable String subject, @Nullable String text, @Nullable String url) {
126         List<String> urlList = new ArrayList<>();
127         if (url != null) {
128             urlList.add(url);
129         }
130         return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, urlList);
131     }
132
133     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
134             @Nullable String text, @Nullable List<String> urlList) {
135         return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, urlList);
136     }
137
138     public static boolean sendMailWithAttachments(ThingActions actions, @Nullable String recipient,
139             @Nullable String subject, @Nullable String text, @Nullable List<String> urlList) {
140         return ((SendMailActions) actions).sendMailWithAttachments(recipient, subject, text, urlList);
141     }
142
143     @RuleAction(label = "@text/sendHTMLMessageActionLabel", description = "@text/sendHTMLMessageActionDescription")
144     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
145             @ActionInput(name = "recipient") @Nullable String recipient,
146             @ActionInput(name = "subject") @Nullable String subject,
147             @ActionInput(name = "htmlContent") @Nullable String htmlContent) {
148         return sendHtmlMailWithAttachments(recipient, subject, htmlContent, List.of());
149     }
150
151     @RuleAction(label = "@text/sendHTMLAttachmentMessageActionLabel", description = "@text/sendHTMLAttachmentMessageActionDescription")
152     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachment(
153             @ActionInput(name = "recipient") @Nullable String recipient,
154             @ActionInput(name = "subject") @Nullable String subject,
155             @ActionInput(name = "htmlContent") @Nullable String htmlContent,
156             @ActionInput(name = "url") @Nullable String url) {
157         List<String> urlList = new ArrayList<>();
158         if (url != null) {
159             urlList.add(url);
160         }
161         return sendHtmlMailWithAttachments(recipient, subject, htmlContent, urlList);
162     }
163
164     @RuleAction(label = "@text/sendHTMLAttachmentsMessageActionLabel", description = "@text/sendHTMLAttachmentsMessageActionDescription")
165     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachments(
166             @ActionInput(name = "recipient") @Nullable String recipient,
167             @ActionInput(name = "subject") @Nullable String subject,
168             @ActionInput(name = "htmlContent") @Nullable String htmlContent,
169             @ActionInput(name = "urlList") @Nullable List<String> urlList) {
170         if (recipient == null) {
171             logger.warn("Cannot send mail as recipient is missing.");
172             return false;
173         }
174
175         try {
176             MailBuilder builder = new MailBuilder(recipient);
177
178             if (subject != null && !subject.isEmpty()) {
179                 builder.withSubject(subject);
180             }
181             if (htmlContent != null && !htmlContent.isEmpty()) {
182                 builder.withHtml(htmlContent);
183             }
184             if (urlList != null) {
185                 for (String urlString : urlList) {
186                     builder.withURLAttachment(urlString);
187                 }
188             }
189
190             headers.forEach((name, value) -> builder.withHeader(name, value));
191
192             final SMTPHandler handler = this.handler;
193             if (handler == null) {
194                 logger.warn("Handler is null, cannot send mail.");
195                 return false;
196             } else {
197                 return handler.sendMail(builder.build());
198             }
199         } catch (AddressException | MalformedURLException | EmailException e) {
200             logger.warn("Could not send mail: {}", e.getMessage());
201             return false;
202         }
203     }
204
205     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
206             @Nullable String htmlContent) {
207         return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, List.of());
208     }
209
210     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
211             @Nullable String htmlContent, @Nullable String url) {
212         return SendMailActions.sendHtmlMailWithAttachment(actions, recipient, subject, htmlContent, url);
213     }
214
215     public static boolean sendHtmlMailWithAttachment(ThingActions actions, @Nullable String recipient,
216             @Nullable String subject, @Nullable String htmlContent, @Nullable String url) {
217         List<String> urlList = new ArrayList<>();
218         if (url != null) {
219             urlList.add(url);
220         }
221         return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, urlList);
222     }
223
224     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
225             @Nullable String htmlContent, @Nullable List<String> urlList) {
226         return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, urlList);
227     }
228
229     public static boolean sendHtmlMailWithAttachments(ThingActions actions, @Nullable String recipient,
230             @Nullable String subject, @Nullable String htmlContent, @Nullable List<String> urlList) {
231         return ((SendMailActions) actions).sendHtmlMailWithAttachments(recipient, subject, htmlContent, urlList);
232     }
233
234     @Override
235     public void setThingHandler(@Nullable ThingHandler handler) {
236         if (handler instanceof SMTPHandler smtpHandler) {
237             this.handler = smtpHandler;
238         }
239     }
240
241     @Override
242     public @Nullable ThingHandler getThingHandler() {
243         return handler;
244     }
245
246     @RuleAction(label = "@text/addHeaderActionLabel", description = "@text/addHeaderActionDescription")
247     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean addHeader(
248             @ActionInput(name = "name") @Nullable String name, @ActionInput(name = "value") @Nullable String value) {
249         if (name != null && !name.isEmpty()) {
250             if (value != null && !value.isEmpty()) {
251                 headers.put(name, value);
252             } else {
253                 headers.remove(name);
254             }
255             return true;
256         }
257         return false;
258     }
259
260     public static boolean addHeader(ThingActions actions, @Nullable String name, @Nullable String value) {
261         return ((SendMailActions) actions).addHeader(name, value);
262     }
263 }