]> git.basschouten.com Git - openhab-addons.git/blob
8fa5201649daed323db3cfd1a3c8b41e8f39c508
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.List;
18
19 import javax.mail.internet.AddressException;
20
21 import org.apache.commons.mail.EmailException;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.mail.internal.MailBuilder;
25 import org.openhab.binding.mail.internal.SMTPHandler;
26 import org.openhab.core.automation.annotation.ActionInput;
27 import org.openhab.core.automation.annotation.ActionOutput;
28 import org.openhab.core.automation.annotation.RuleAction;
29 import org.openhab.core.thing.binding.ThingActions;
30 import org.openhab.core.thing.binding.ThingActionsScope;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link SendMailActions} class defines rule actions for sending mail
37  *
38  * @author Jan N. Klug - Initial contribution
39  */
40 @ThingActionsScope(name = "mail")
41 @NonNullByDefault
42 public class SendMailActions implements ThingActions {
43
44     private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
45
46     private @Nullable SMTPHandler handler;
47
48     @RuleAction(label = "@text/sendMessageActionLabel", description = "@text/sendMessageActionDescription")
49     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
50             @ActionInput(name = "recipient") @Nullable String recipient,
51             @ActionInput(name = "subject") @Nullable String subject,
52             @ActionInput(name = "text") @Nullable String text) {
53         return sendMailWithAttachments(recipient, subject, text, List.of());
54     }
55
56     @RuleAction(label = "@text/sendAttachmentMessageActionLabel", description = "@text/sendAttachmentMessageActionDescription")
57     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMailWithAttachment(
58             @ActionInput(name = "recipient") @Nullable String recipient,
59             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
60             @ActionInput(name = "url") @Nullable String urlString) {
61         List<String> urlList = new ArrayList<>();
62         if (urlString != null) {
63             urlList.add(urlString);
64         }
65         return sendMailWithAttachments(recipient, subject, text, urlList);
66     }
67
68     @RuleAction(label = "@text/sendAttachmentsMessageActionLabel", description = "@text/sendAttachmentsMessageActionDescription")
69     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMailWithAttachments(
70             @ActionInput(name = "recipient") @Nullable String recipient,
71             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
72             @ActionInput(name = "urlList") @Nullable List<String> urlStringList) {
73         if (recipient == null) {
74             logger.warn("Cannot send mail as recipient is missing.");
75             return false;
76         }
77
78         try {
79             MailBuilder builder = new MailBuilder(recipient);
80
81             if (subject != null && !subject.isEmpty()) {
82                 builder.withSubject(subject);
83             }
84             if (text != null && !text.isEmpty()) {
85                 builder.withText(text);
86             }
87             if (urlStringList != null) {
88                 for (String urlString : urlStringList) {
89                     builder.withURLAttachment(urlString);
90                 }
91             }
92
93             final SMTPHandler handler = this.handler;
94             if (handler == null) {
95                 logger.info("Handler is null, cannot send mail.");
96                 return false;
97             } else {
98                 return handler.sendMail(builder.build());
99             }
100         } catch (AddressException | MalformedURLException | EmailException e) {
101             logger.warn("Could not send mail: {}", e.getMessage());
102             return false;
103         }
104     }
105
106     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
107             @Nullable String text) {
108         return SendMailActions.sendMail(actions, recipient, subject, text, List.of());
109     }
110
111     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
112             @Nullable String text, @Nullable String urlString) {
113         List<String> urlList = new ArrayList<>();
114         if (urlString != null) {
115             urlList.add(urlString);
116         }
117         return SendMailActions.sendMail(actions, recipient, subject, text, urlList);
118     }
119
120     public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
121             @Nullable String text, @Nullable List<String> urlStringList) {
122         return ((SendMailActions) actions).sendMailWithAttachments(recipient, subject, text, urlStringList);
123     }
124
125     @RuleAction(label = "@text/sendHTMLMessageActionLabel", description = "@text/sendHTMLMessageActionDescription")
126     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
127             @ActionInput(name = "recipient") @Nullable String recipient,
128             @ActionInput(name = "subject") @Nullable String subject,
129             @ActionInput(name = "html") @Nullable String html) {
130         return sendHtmlMailWithAttachments(recipient, subject, html, List.of());
131     }
132
133     @RuleAction(label = "@text/sendHTMLAttachmentMessageActionLabel", description = "@text/sendHTMLAttachmentMessageActionDescription")
134     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachment(
135             @ActionInput(name = "recipient") @Nullable String recipient,
136             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
137             @ActionInput(name = "url") @Nullable String urlString) {
138         List<String> urlList = new ArrayList<>();
139         if (urlString != null) {
140             urlList.add(urlString);
141         }
142         return sendHtmlMailWithAttachments(recipient, subject, html, urlList);
143     }
144
145     @RuleAction(label = "@text/sendHTMLAttachmentsMessageActionLabel", description = "@text/sendHTMLAttachmentsMessageActionDescription")
146     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachments(
147             @ActionInput(name = "recipient") @Nullable String recipient,
148             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
149             @ActionInput(name = "urlList") @Nullable List<String> urlStringList) {
150         if (recipient == null) {
151             logger.warn("Cannot send mail as recipient is missing.");
152             return false;
153         }
154
155         try {
156             MailBuilder builder = new MailBuilder(recipient);
157
158             if (subject != null && !subject.isEmpty()) {
159                 builder.withSubject(subject);
160             }
161             if (html != null && !html.isEmpty()) {
162                 builder.withHtml(html);
163             }
164             if (urlStringList != null) {
165                 for (String urlString : urlStringList) {
166                     builder.withURLAttachment(urlString);
167                 }
168             }
169
170             final SMTPHandler handler = this.handler;
171             if (handler == null) {
172                 logger.warn("Handler is null, cannot send mail.");
173                 return false;
174             } else {
175                 return handler.sendMail(builder.build());
176             }
177         } catch (AddressException | MalformedURLException | EmailException e) {
178             logger.warn("Could not send mail: {}", e.getMessage());
179             return false;
180         }
181     }
182
183     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
184             @Nullable String html) {
185         return SendMailActions.sendHtmlMail(actions, recipient, subject, html, List.of());
186     }
187
188     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
189             @Nullable String html, @Nullable String urlString) {
190         List<String> urlList = new ArrayList<>();
191         if (urlString != null) {
192             urlList.add(urlString);
193         }
194         return SendMailActions.sendHtmlMail(actions, recipient, subject, html, urlList);
195     }
196
197     public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
198             @Nullable String html, @Nullable List<String> urlStringList) {
199         return ((SendMailActions) actions).sendHtmlMailWithAttachments(recipient, subject, html, urlStringList);
200     }
201
202     @Override
203     public void setThingHandler(@Nullable ThingHandler handler) {
204         if (handler instanceof SMTPHandler) {
205             this.handler = (SMTPHandler) handler;
206         }
207     }
208
209     @Override
210     public @Nullable ThingHandler getThingHandler() {
211         return handler;
212     }
213 }