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 = "@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());
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);
65 return sendMailWithAttachments(recipient, subject, text, urlList);
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.");
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(ThingActions actions, @Nullable String recipient, @Nullable String subject,
107 @Nullable String text) {
108 return SendMailActions.sendMail(actions, recipient, subject, text, List.of());
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);
117 return SendMailActions.sendMail(actions, recipient, subject, text, urlList);
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);
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());
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);
142 return sendHtmlMailWithAttachments(recipient, subject, html, urlList);
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.");
156 MailBuilder builder = new MailBuilder(recipient);
158 if (subject != null && !subject.isEmpty()) {
159 builder.withSubject(subject);
161 if (html != null && !html.isEmpty()) {
162 builder.withHtml(html);
164 if (urlStringList != null) {
165 for (String urlString : urlStringList) {
166 builder.withURLAttachment(urlString);
170 final SMTPHandler handler = this.handler;
171 if (handler == null) {
172 logger.warn("Handler is null, cannot send mail.");
175 return handler.sendMail(builder.build());
177 } catch (AddressException | MalformedURLException | EmailException e) {
178 logger.warn("Could not send mail: {}", e.getMessage());
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());
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);
194 return SendMailActions.sendHtmlMail(actions, recipient, subject, html, urlList);
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);
203 public void setThingHandler(@Nullable ThingHandler handler) {
204 if (handler instanceof SMTPHandler) {
205 this.handler = (SMTPHandler) handler;
210 public @Nullable ThingHandler getThingHandler() {