]> git.basschouten.com Git - openhab-addons.git/blob
8c27b734387b2920899f25e2517c28d30571cf78
[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 url) {
64         List<String> urlList = new ArrayList<>();
65         if (url != null) {
66             urlList.add(url);
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> urlList) {
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 (urlList != null) {
91                 for (String urlString : urlList) {
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.sendMailWithAttachments(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 url) {
118         return SendMailActions.sendMailWithAttachment(actions, recipient, subject, text, url);
119     }
120
121     public static boolean sendMailWithAttachment(ThingActions actions, @Nullable String recipient,
122             @Nullable String subject, @Nullable String text, @Nullable String url) {
123         List<String> urlList = new ArrayList<>();
124         if (url != null) {
125             urlList.add(url);
126         }
127         return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, urlList);
128     }
129
130     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
131             @Nullable String text, @Nullable List<String> urlList) {
132         return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, urlList);
133     }
134
135     public static boolean sendMailWithAttachments(ThingActions actions, @Nullable String recipient,
136             @Nullable String subject, @Nullable String text, @Nullable List<String> urlList) {
137         return ((SendMailActions) actions).sendMailWithAttachments(recipient, subject, text, urlList);
138     }
139
140     @RuleAction(label = "@text/sendHTMLMessageActionLabel", description = "@text/sendHTMLMessageActionDescription")
141     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
142             @ActionInput(name = "recipient") @Nullable String recipient,
143             @ActionInput(name = "subject") @Nullable String subject,
144             @ActionInput(name = "htmlContent") @Nullable String htmlContent) {
145         return sendHtmlMailWithAttachments(recipient, subject, htmlContent, List.of());
146     }
147
148     @RuleAction(label = "@text/sendHTMLAttachmentMessageActionLabel", description = "@text/sendHTMLAttachmentMessageActionDescription")
149     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachment(
150             @ActionInput(name = "recipient") @Nullable String recipient,
151             @ActionInput(name = "subject") @Nullable String subject,
152             @ActionInput(name = "htmlContent") @Nullable String htmlContent,
153             @ActionInput(name = "url") @Nullable String url) {
154         List<String> urlList = new ArrayList<>();
155         if (url != null) {
156             urlList.add(url);
157         }
158         return sendHtmlMailWithAttachments(recipient, subject, htmlContent, urlList);
159     }
160
161     @RuleAction(label = "@text/sendHTMLAttachmentsMessageActionLabel", description = "@text/sendHTMLAttachmentsMessageActionDescription")
162     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachments(
163             @ActionInput(name = "recipient") @Nullable String recipient,
164             @ActionInput(name = "subject") @Nullable String subject,
165             @ActionInput(name = "htmlContent") @Nullable String htmlContent,
166             @ActionInput(name = "urlList") @Nullable List<String> urlList) {
167         if (recipient == null) {
168             logger.warn("Cannot send mail as recipient is missing.");
169             return false;
170         }
171
172         try {
173             MailBuilder builder = new MailBuilder(recipient);
174
175             if (subject != null && !subject.isEmpty()) {
176                 builder.withSubject(subject);
177             }
178             if (htmlContent != null && !htmlContent.isEmpty()) {
179                 builder.withHtml(htmlContent);
180             }
181             if (urlList != null) {
182                 for (String urlString : urlList) {
183                     builder.withURLAttachment(urlString);
184                 }
185             }
186
187             headers.forEach((name, value) -> builder.withHeader(name, value));
188
189             final SMTPHandler handler = this.handler;
190             if (handler == null) {
191                 logger.warn("Handler is null, cannot send mail.");
192                 return false;
193             } else {
194                 return handler.sendMail(builder.build());
195             }
196         } catch (AddressException | MalformedURLException | EmailException e) {
197             logger.warn("Could not send mail: {}", e.getMessage());
198             return false;
199         }
200     }
201
202     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
203             @Nullable String htmlContent) {
204         return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, List.of());
205     }
206
207     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
208             @Nullable String htmlContent, @Nullable String url) {
209         return SendMailActions.sendHtmlMailWithAttachment(actions, recipient, subject, htmlContent, url);
210     }
211
212     public static boolean sendHtmlMailWithAttachment(ThingActions actions, @Nullable String recipient,
213             @Nullable String subject, @Nullable String htmlContent, @Nullable String url) {
214         List<String> urlList = new ArrayList<>();
215         if (url != null) {
216             urlList.add(url);
217         }
218         return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, urlList);
219     }
220
221     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
222             @Nullable String htmlContent, @Nullable List<String> urlList) {
223         return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, urlList);
224     }
225
226     public static boolean sendHtmlMailWithAttachments(ThingActions actions, @Nullable String recipient,
227             @Nullable String subject, @Nullable String htmlContent, @Nullable List<String> urlList) {
228         return ((SendMailActions) actions).sendHtmlMailWithAttachments(recipient, subject, htmlContent, urlList);
229     }
230
231     @Override
232     public void setThingHandler(@Nullable ThingHandler handler) {
233         if (handler instanceof SMTPHandler smtpHandler) {
234             this.handler = smtpHandler;
235         }
236     }
237
238     @Override
239     public @Nullable ThingHandler getThingHandler() {
240         return handler;
241     }
242
243     @RuleAction(label = "@text/addHeaderActionLabel", description = "@text/addHeaderActionDescription")
244     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean addHeader(
245             @ActionInput(name = "name") @Nullable String name, @ActionInput(name = "value") @Nullable String value) {
246         if (name != null && !name.isEmpty()) {
247             if (value != null && !value.isEmpty()) {
248                 headers.put(name, value);
249             } else {
250                 headers.remove(name);
251             }
252             return true;
253         }
254         return false;
255     }
256
257     public static boolean addHeader(ThingActions actions, @Nullable String name, @Nullable String value) {
258         return ((SendMailActions) actions).addHeader(name, value);
259     }
260 }