]> git.basschouten.com Git - openhab-addons.git/blob
2c86e18cefd6a87f12a88b8c342305590d041b2d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 = "send a text mail", description = "Sends a text mail.")
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 sendMail(recipient, subject, text, new ArrayList<>());
54     }
55
56     @RuleAction(label = "send a text mail", description = "Sends a text mail with URL attachment.")
57     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
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 sendMail(recipient, subject, text, urlList);
66     }
67
68     @RuleAction(label = "send a text mail", description = "Sends a text mail with several URL attachments.")
69     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
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(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String subject,
107             @Nullable String text) {
108         return SendMailActions.sendMail(actions, recipient, subject, text, new ArrayList<>());
109     }
110
111     public static boolean sendMail(@Nullable 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(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String subject,
121             @Nullable String text, @Nullable List<String> urlStringList) {
122         if (actions instanceof SendMailActions) {
123             return ((SendMailActions) actions).sendMail(recipient, subject, text, urlStringList);
124         } else {
125             throw new IllegalArgumentException("Actions is not an instance of SendMailActions");
126         }
127     }
128
129     @RuleAction(label = "send a HTML mail", description = "Sends a HTML mail.")
130     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
131             @ActionInput(name = "recipient") @Nullable String recipient,
132             @ActionInput(name = "subject") @Nullable String subject,
133             @ActionInput(name = "html") @Nullable String html) {
134         return sendHtmlMail(recipient, subject, html, new ArrayList<>());
135     }
136
137     @RuleAction(label = "send a HTML mail", description = "Sends a HTML mail with URL attachment.")
138     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
139             @ActionInput(name = "recipient") @Nullable String recipient,
140             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
141             @ActionInput(name = "url") @Nullable String urlString) {
142         List<String> urlList = new ArrayList<>();
143         if (urlString != null) {
144             urlList.add(urlString);
145         }
146         return sendHtmlMail(recipient, subject, html, urlList);
147     }
148
149     @RuleAction(label = "send a HTML mail", description = "Sends a HTML mail with several URL attachments.")
150     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
151             @ActionInput(name = "recipient") @Nullable String recipient,
152             @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
153             @ActionInput(name = "urlList") @Nullable List<String> urlStringList) {
154         if (recipient == null) {
155             logger.warn("Cannot send mail as recipient is missing.");
156             return false;
157         }
158
159         try {
160             MailBuilder builder = new MailBuilder(recipient);
161
162             if (subject != null && !subject.isEmpty()) {
163                 builder.withSubject(subject);
164             }
165             if (html != null && !html.isEmpty()) {
166                 builder.withHtml(html);
167             }
168             if (urlStringList != null) {
169                 for (String urlString : urlStringList) {
170                     builder.withURLAttachment(urlString);
171                 }
172             }
173
174             final SMTPHandler handler = this.handler;
175             if (handler == null) {
176                 logger.warn("Handler is null, cannot send mail.");
177                 return false;
178             } else {
179                 return handler.sendMail(builder.build());
180             }
181         } catch (AddressException | MalformedURLException | EmailException e) {
182             logger.warn("Could not send mail: {}", e.getMessage());
183             return false;
184         }
185     }
186
187     public static boolean sendHtmlMail(@Nullable ThingActions actions, @Nullable String recipient,
188             @Nullable String subject, @Nullable String html) {
189         return SendMailActions.sendHtmlMail(actions, recipient, subject, html, new ArrayList<>());
190     }
191
192     public static boolean sendHtmlMail(@Nullable ThingActions actions, @Nullable String recipient,
193             @Nullable String subject, @Nullable String html, @Nullable String urlString) {
194         List<String> urlList = new ArrayList<>();
195         if (urlString != null) {
196             urlList.add(urlString);
197         }
198         return SendMailActions.sendHtmlMail(actions, recipient, subject, html, urlList);
199     }
200
201     public static boolean sendHtmlMail(@Nullable ThingActions actions, @Nullable String recipient,
202             @Nullable String subject, @Nullable String html, @Nullable List<String> urlStringList) {
203         if (actions instanceof SendMailActions) {
204             return ((SendMailActions) actions).sendHtmlMail(recipient, subject, html, urlStringList);
205         } else {
206             throw new IllegalArgumentException("Actions is not an instance of SendMailActions");
207         }
208     }
209
210     @Override
211     public void setThingHandler(@Nullable ThingHandler handler) {
212         if (handler instanceof SMTPHandler) {
213             this.handler = (SMTPHandler) handler;
214         }
215     }
216
217     @Override
218     public @Nullable ThingHandler getThingHandler() {
219         return handler;
220     }
221 }