2 * Copyright (c) 2010-2024 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 url) {
64 List<String> urlList = new ArrayList<>();
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> urlList) {
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 (urlList != null) {
91 for (String urlString : urlList) {
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.sendMailWithAttachments(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 url) {
118 return SendMailActions.sendMailWithAttachment(actions, recipient, subject, text, url);
121 public static boolean sendMailWithAttachment(ThingActions actions, @Nullable String recipient,
122 @Nullable String subject, @Nullable String text, @Nullable String url) {
123 List<String> urlList = new ArrayList<>();
127 return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, urlList);
130 public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
131 @Nullable String text, @Nullable List<String> urlList) {
132 return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, urlList);
135 public static boolean sendMailWithAttachments(ThingActions actions, @Nullable String recipient,
136 @Nullable String subject, @Nullable String text, @Nullable List<String> urlList) {
137 return ((SendMailActions) actions).sendMailWithAttachments(recipient, subject, text, urlList);
140 @RuleAction(label = "@text/sendHTMLMessageActionLabel", description = "@text/sendHTMLMessageActionDescription")
141 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
142 @ActionInput(name = "recipient") @Nullable String recipient,
143 @ActionInput(name = "subject") @Nullable String subject,
144 @ActionInput(name = "htmlContent") @Nullable String htmlContent) {
145 return sendHtmlMailWithAttachments(recipient, subject, htmlContent, List.of());
148 @RuleAction(label = "@text/sendHTMLAttachmentMessageActionLabel", description = "@text/sendHTMLAttachmentMessageActionDescription")
149 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachment(
150 @ActionInput(name = "recipient") @Nullable String recipient,
151 @ActionInput(name = "subject") @Nullable String subject,
152 @ActionInput(name = "htmlContent") @Nullable String htmlContent,
153 @ActionInput(name = "url") @Nullable String url) {
154 List<String> urlList = new ArrayList<>();
158 return sendHtmlMailWithAttachments(recipient, subject, htmlContent, urlList);
161 @RuleAction(label = "@text/sendHTMLAttachmentsMessageActionLabel", description = "@text/sendHTMLAttachmentsMessageActionDescription")
162 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachments(
163 @ActionInput(name = "recipient") @Nullable String recipient,
164 @ActionInput(name = "subject") @Nullable String subject,
165 @ActionInput(name = "htmlContent") @Nullable String htmlContent,
166 @ActionInput(name = "urlList") @Nullable List<String> urlList) {
167 if (recipient == null) {
168 logger.warn("Cannot send mail as recipient is missing.");
173 MailBuilder builder = new MailBuilder(recipient);
175 if (subject != null && !subject.isEmpty()) {
176 builder.withSubject(subject);
178 if (htmlContent != null && !htmlContent.isEmpty()) {
179 builder.withHtml(htmlContent);
181 if (urlList != null) {
182 for (String urlString : urlList) {
183 builder.withURLAttachment(urlString);
187 headers.forEach((name, value) -> builder.withHeader(name, value));
189 final SMTPHandler handler = this.handler;
190 if (handler == null) {
191 logger.warn("Handler is null, cannot send mail.");
194 return handler.sendMail(builder.build());
196 } catch (AddressException | MalformedURLException | EmailException e) {
197 logger.warn("Could not send mail: {}", e.getMessage());
202 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
203 @Nullable String htmlContent) {
204 return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, List.of());
207 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
208 @Nullable String htmlContent, @Nullable String url) {
209 return SendMailActions.sendHtmlMailWithAttachment(actions, recipient, subject, htmlContent, url);
212 public static boolean sendHtmlMailWithAttachment(ThingActions actions, @Nullable String recipient,
213 @Nullable String subject, @Nullable String htmlContent, @Nullable String url) {
214 List<String> urlList = new ArrayList<>();
218 return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, urlList);
221 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
222 @Nullable String htmlContent, @Nullable List<String> urlList) {
223 return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, urlList);
226 public static boolean sendHtmlMailWithAttachments(ThingActions actions, @Nullable String recipient,
227 @Nullable String subject, @Nullable String htmlContent, @Nullable List<String> urlList) {
228 return ((SendMailActions) actions).sendHtmlMailWithAttachments(recipient, subject, htmlContent, urlList);
232 public void setThingHandler(@Nullable ThingHandler handler) {
233 if (handler instanceof SMTPHandler smtpHandler) {
234 this.handler = smtpHandler;
239 public @Nullable ThingHandler getThingHandler() {
243 @RuleAction(label = "@text/addHeaderActionLabel", description = "@text/addHeaderActionDescription")
244 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean addHeader(
245 @ActionInput(name = "name") @Nullable String name, @ActionInput(name = "value") @Nullable String value) {
246 if (name != null && !name.isEmpty()) {
247 if (value != null && !value.isEmpty()) {
248 headers.put(name, value);
250 headers.remove(name);
257 public static boolean addHeader(ThingActions actions, @Nullable String name, @Nullable String value) {
258 return ((SendMailActions) actions).addHeader(name, value);