2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mail.internal.action;
15 import java.net.MalformedURLException;
16 import java.util.ArrayList;
17 import java.util.List;
19 import javax.mail.internet.AddressException;
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;
36 * The {@link SendMailActions} class defines rule actions for sending mail
38 * @author Jan N. Klug - Initial contribution
40 @ThingActionsScope(name = "mail")
42 public class SendMailActions implements ThingActions {
44 private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
46 private @Nullable SMTPHandler handler;
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<>());
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);
65 return sendMail(recipient, subject, text, urlList);
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.");
79 MailBuilder builder = new MailBuilder(recipient);
81 if (subject != null && !subject.isEmpty()) {
82 builder.withSubject(subject);
84 if (text != null && !text.isEmpty()) {
85 builder.withText(text);
87 if (urlStringList != null) {
88 for (String urlString : urlStringList) {
89 builder.withURLAttachment(urlString);
93 final SMTPHandler handler = this.handler;
94 if (handler == null) {
95 logger.info("Handler is null, cannot send mail.");
98 return handler.sendMail(builder.build());
100 } catch (AddressException | MalformedURLException | EmailException e) {
101 logger.warn("Could not send mail: {}", e.getMessage());
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<>());
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);
117 return SendMailActions.sendMail(actions, recipient, subject, text, urlList);
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);
125 throw new IllegalArgumentException("Actions is not an instance of SendMailActions");
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<>());
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);
146 return sendHtmlMail(recipient, subject, html, urlList);
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.");
160 MailBuilder builder = new MailBuilder(recipient);
162 if (subject != null && !subject.isEmpty()) {
163 builder.withSubject(subject);
165 if (html != null && !html.isEmpty()) {
166 builder.withHtml(html);
168 if (urlStringList != null) {
169 for (String urlString : urlStringList) {
170 builder.withURLAttachment(urlString);
174 final SMTPHandler handler = this.handler;
175 if (handler == null) {
176 logger.warn("Handler is null, cannot send mail.");
179 return handler.sendMail(builder.build());
181 } catch (AddressException | MalformedURLException | EmailException e) {
182 logger.warn("Could not send mail: {}", e.getMessage());
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<>());
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);
198 return SendMailActions.sendHtmlMail(actions, recipient, subject, html, urlList);
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);
206 throw new IllegalArgumentException("Actions is not an instance of SendMailActions");
211 public void setThingHandler(@Nullable ThingHandler handler) {
212 if (handler instanceof SMTPHandler) {
213 this.handler = (SMTPHandler) handler;
218 public @Nullable ThingHandler getThingHandler() {