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.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.ServiceScope;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link SendMailActions} class defines rule actions for sending mail
42 * @author Jan N. Klug - Initial contribution
44 @Component(scope = ServiceScope.PROTOTYPE, service = SendMailActions.class)
45 @ThingActionsScope(name = "mail")
47 public class SendMailActions implements ThingActions {
49 private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
51 private @Nullable SMTPHandler handler;
52 private Map<String, String> headers = new HashMap<>();
54 @RuleAction(label = "@text/sendMessageActionLabel", description = "@text/sendMessageActionDescription")
55 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
56 @ActionInput(name = "recipient") @Nullable String recipient,
57 @ActionInput(name = "subject") @Nullable String subject,
58 @ActionInput(name = "text") @Nullable String text) {
59 return sendMailWithAttachments(recipient, subject, text, List.of());
62 @RuleAction(label = "@text/sendAttachmentMessageActionLabel", description = "@text/sendAttachmentMessageActionDescription")
63 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMailWithAttachment(
64 @ActionInput(name = "recipient") @Nullable String recipient,
65 @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
66 @ActionInput(name = "url") @Nullable String url) {
67 List<String> urlList = new ArrayList<>();
71 return sendMailWithAttachments(recipient, subject, text, urlList);
74 @RuleAction(label = "@text/sendAttachmentsMessageActionLabel", description = "@text/sendAttachmentsMessageActionDescription")
75 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMailWithAttachments(
76 @ActionInput(name = "recipient") @Nullable String recipient,
77 @ActionInput(name = "subject") @Nullable String subject, @ActionInput(name = "text") @Nullable String text,
78 @ActionInput(name = "urlList") @Nullable List<String> urlList) {
79 if (recipient == null) {
80 logger.warn("Cannot send mail as recipient is missing.");
85 MailBuilder builder = new MailBuilder(recipient);
87 if (subject != null && !subject.isEmpty()) {
88 builder.withSubject(subject);
90 if (text != null && !text.isEmpty()) {
91 builder.withText(text);
93 if (urlList != null) {
94 for (String urlString : urlList) {
95 builder.withURLAttachment(urlString);
99 headers.forEach((name, value) -> builder.withHeader(name, value));
101 final SMTPHandler handler = this.handler;
102 if (handler == null) {
103 logger.info("Handler is null, cannot send mail.");
106 return handler.sendMail(builder.build());
108 } catch (AddressException | MalformedURLException | EmailException e) {
109 logger.warn("Could not send mail: {}", e.getMessage());
114 public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
115 @Nullable String text) {
116 return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, List.of());
119 public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
120 @Nullable String text, @Nullable String url) {
121 return SendMailActions.sendMailWithAttachment(actions, recipient, subject, text, url);
124 public static boolean sendMailWithAttachment(ThingActions actions, @Nullable String recipient,
125 @Nullable String subject, @Nullable String text, @Nullable String url) {
126 List<String> urlList = new ArrayList<>();
130 return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, urlList);
133 public static boolean sendMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
134 @Nullable String text, @Nullable List<String> urlList) {
135 return SendMailActions.sendMailWithAttachments(actions, recipient, subject, text, urlList);
138 public static boolean sendMailWithAttachments(ThingActions actions, @Nullable String recipient,
139 @Nullable String subject, @Nullable String text, @Nullable List<String> urlList) {
140 return ((SendMailActions) actions).sendMailWithAttachments(recipient, subject, text, urlList);
143 @RuleAction(label = "@text/sendHTMLMessageActionLabel", description = "@text/sendHTMLMessageActionDescription")
144 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMail(
145 @ActionInput(name = "recipient") @Nullable String recipient,
146 @ActionInput(name = "subject") @Nullable String subject,
147 @ActionInput(name = "htmlContent") @Nullable String htmlContent) {
148 return sendHtmlMailWithAttachments(recipient, subject, htmlContent, List.of());
151 @RuleAction(label = "@text/sendHTMLAttachmentMessageActionLabel", description = "@text/sendHTMLAttachmentMessageActionDescription")
152 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachment(
153 @ActionInput(name = "recipient") @Nullable String recipient,
154 @ActionInput(name = "subject") @Nullable String subject,
155 @ActionInput(name = "htmlContent") @Nullable String htmlContent,
156 @ActionInput(name = "url") @Nullable String url) {
157 List<String> urlList = new ArrayList<>();
161 return sendHtmlMailWithAttachments(recipient, subject, htmlContent, urlList);
164 @RuleAction(label = "@text/sendHTMLAttachmentsMessageActionLabel", description = "@text/sendHTMLAttachmentsMessageActionDescription")
165 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendHtmlMailWithAttachments(
166 @ActionInput(name = "recipient") @Nullable String recipient,
167 @ActionInput(name = "subject") @Nullable String subject,
168 @ActionInput(name = "htmlContent") @Nullable String htmlContent,
169 @ActionInput(name = "urlList") @Nullable List<String> urlList) {
170 if (recipient == null) {
171 logger.warn("Cannot send mail as recipient is missing.");
176 MailBuilder builder = new MailBuilder(recipient);
178 if (subject != null && !subject.isEmpty()) {
179 builder.withSubject(subject);
181 if (htmlContent != null && !htmlContent.isEmpty()) {
182 builder.withHtml(htmlContent);
184 if (urlList != null) {
185 for (String urlString : urlList) {
186 builder.withURLAttachment(urlString);
190 headers.forEach((name, value) -> builder.withHeader(name, value));
192 final SMTPHandler handler = this.handler;
193 if (handler == null) {
194 logger.warn("Handler is null, cannot send mail.");
197 return handler.sendMail(builder.build());
199 } catch (AddressException | MalformedURLException | EmailException e) {
200 logger.warn("Could not send mail: {}", e.getMessage());
205 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
206 @Nullable String htmlContent) {
207 return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, List.of());
210 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
211 @Nullable String htmlContent, @Nullable String url) {
212 return SendMailActions.sendHtmlMailWithAttachment(actions, recipient, subject, htmlContent, url);
215 public static boolean sendHtmlMailWithAttachment(ThingActions actions, @Nullable String recipient,
216 @Nullable String subject, @Nullable String htmlContent, @Nullable String url) {
217 List<String> urlList = new ArrayList<>();
221 return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, urlList);
224 public static boolean sendHtmlMail(ThingActions actions, @Nullable String recipient, @Nullable String subject,
225 @Nullable String htmlContent, @Nullable List<String> urlList) {
226 return SendMailActions.sendHtmlMailWithAttachments(actions, recipient, subject, htmlContent, urlList);
229 public static boolean sendHtmlMailWithAttachments(ThingActions actions, @Nullable String recipient,
230 @Nullable String subject, @Nullable String htmlContent, @Nullable List<String> urlList) {
231 return ((SendMailActions) actions).sendHtmlMailWithAttachments(recipient, subject, htmlContent, urlList);
235 public void setThingHandler(@Nullable ThingHandler handler) {
236 if (handler instanceof SMTPHandler smtpHandler) {
237 this.handler = smtpHandler;
242 public @Nullable ThingHandler getThingHandler() {
246 @RuleAction(label = "@text/addHeaderActionLabel", description = "@text/addHeaderActionDescription")
247 public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean addHeader(
248 @ActionInput(name = "name") @Nullable String name, @ActionInput(name = "value") @Nullable String value) {
249 if (name != null && !name.isEmpty()) {
250 if (value != null && !value.isEmpty()) {
251 headers.put(name, value);
253 headers.remove(name);
260 public static boolean addHeader(ThingActions actions, @Nullable String name, @Nullable String value) {
261 return ((SendMailActions) actions).addHeader(name, value);