2 * Copyright (c) 2010-2023 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.HashMap;
18 import java.util.List;
21 import javax.mail.internet.AddressException;
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;
38 * The {@link SendMailActions} class defines rule actions for sending mail
40 * @author Jan N. Klug - Initial contribution
42 @ThingActionsScope(name = "mail")
44 public class SendMailActions implements ThingActions {
46 private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
48 private @Nullable SMTPHandler handler;
49 private Map<String, String> headers = new HashMap<>();
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());
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 urlString) {
64 List<String> urlList = new ArrayList<>();
65 if (urlString != null) {
66 urlList.add(urlString);
68 return sendMailWithAttachments(recipient, subject, text, urlList);
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> urlStringList) {
76 if (recipient == null) {
77 logger.warn("Cannot send mail as recipient is missing.");
82 MailBuilder builder = new MailBuilder(recipient);
84 if (subject != null && !subject.isEmpty()) {
85 builder.withSubject(subject);
87 if (text != null && !text.isEmpty()) {
88 builder.withText(text);
90 if (urlStringList != null) {
91 for (String urlString : urlStringList) {
92 builder.withURLAttachment(urlString);
96 headers.forEach((name, value) -> builder.withHeader(name, value));
98 final SMTPHandler handler = this.handler;
99 if (handler == null) {
100 logger.info("Handler is null, cannot send mail.");
103 return handler.sendMail(builder.build());
105 } catch (AddressException | MalformedURLException | EmailException e) {
106 logger.warn("Could not send mail: {}", e.getMessage());
111 public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
112 @Nullable String text) {
113 return SendMailActions.sendMail(actions, recipient, subject, text, List.of());
116 public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
117 @Nullable String text, @Nullable String urlString) {
118 List<String> urlList = new ArrayList<>();
119 if (urlString != null) {
120 urlList.add(urlString);
122 return SendMailActions.sendMail(actions, recipient, subject, text, urlList);
125 public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
126 @Nullable String text, @Nullable List<String> urlStringList) {
127 return ((SendMailActions) actions).sendMailWithAttachments(recipient, subject, text, urlStringList);
130 @RuleAction(label = "@text/sendHTMLMessageActionLabel", description = "@text/sendHTMLMessageActionDescription")
131 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
132 @ActionInput(name = "recipient") @Nullable String recipient,
133 @ActionInput(name = "subject") @Nullable String subject,
134 @ActionInput(name = "html") @Nullable String html) {
135 return sendHtmlMailWithAttachments(recipient, subject, html, List.of());
138 @RuleAction(label = "@text/sendHTMLAttachmentMessageActionLabel", description = "@text/sendHTMLAttachmentMessageActionDescription")
139 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachment(
140 @ActionInput(name = "recipient") @Nullable String recipient,
141 @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
142 @ActionInput(name = "url") @Nullable String urlString) {
143 List<String> urlList = new ArrayList<>();
144 if (urlString != null) {
145 urlList.add(urlString);
147 return sendHtmlMailWithAttachments(recipient, subject, html, urlList);
150 @RuleAction(label = "@text/sendHTMLAttachmentsMessageActionLabel", description = "@text/sendHTMLAttachmentsMessageActionDescription")
151 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachments(
152 @ActionInput(name = "recipient") @Nullable String recipient,
153 @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "html") @Nullable String html,
154 @ActionInput(name = "urlList") @Nullable List<String> urlStringList) {
155 if (recipient == null) {
156 logger.warn("Cannot send mail as recipient is missing.");
161 MailBuilder builder = new MailBuilder(recipient);
163 if (subject != null && !subject.isEmpty()) {
164 builder.withSubject(subject);
166 if (html != null && !html.isEmpty()) {
167 builder.withHtml(html);
169 if (urlStringList != null) {
170 for (String urlString : urlStringList) {
171 builder.withURLAttachment(urlString);
175 headers.forEach((name, value) -> builder.withHeader(name, value));
177 final SMTPHandler handler = this.handler;
178 if (handler == null) {
179 logger.warn("Handler is null, cannot send mail.");
182 return handler.sendMail(builder.build());
184 } catch (AddressException | MalformedURLException | EmailException e) {
185 logger.warn("Could not send mail: {}", e.getMessage());
190 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
191 @Nullable String html) {
192 return SendMailActions.sendHtmlMail(actions, recipient, subject, html, List.of());
195 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
196 @Nullable String html, @Nullable String urlString) {
197 List<String> urlList = new ArrayList<>();
198 if (urlString != null) {
199 urlList.add(urlString);
201 return SendMailActions.sendHtmlMail(actions, recipient, subject, html, urlList);
204 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
205 @Nullable String html, @Nullable List<String> urlStringList) {
206 return ((SendMailActions) actions).sendHtmlMailWithAttachments(recipient, subject, html, urlStringList);
210 public void setThingHandler(@Nullable ThingHandler handler) {
211 if (handler instanceof SMTPHandler) {
212 this.handler = (SMTPHandler) handler;
217 public @Nullable ThingHandler getThingHandler() {
221 @RuleAction(label = "@text/addHeaderActionLabel", description = "@text/addHeaderActionDescription")
222 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean addHeader(
223 @ActionInput(name = "name") @Nullable String name, @ActionInput(name = "value") @Nullable String value) {
224 if (name != null && !name.isEmpty()) {
225 if (value != null && !value.isEmpty()) {
226 headers.put(name, value);
228 headers.remove(name);
235 public static boolean addHeader(ThingActions actions, @Nullable String name, @Nullable String value) {
236 return ((SendMailActions) actions).addHeader(name, value);