001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.server.mail;
021
022import jakarta.annotation.Nonnull;
023import org.apache.commons.lang3.Validate;
024import org.simplejavamail.MailException;
025import org.simplejavamail.api.email.Email;
026import org.simplejavamail.api.email.Recipient;
027import org.simplejavamail.api.mailer.AsyncResponse;
028import org.simplejavamail.api.mailer.AsyncResponse.ExceptionConsumer;
029import org.simplejavamail.api.mailer.Mailer;
030import org.simplejavamail.api.mailer.config.TransportStrategy;
031import org.simplejavamail.mailer.MailerBuilder;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035import java.util.List;
036import java.util.stream.Collectors;
037
038public class MailSvc implements IMailSvc {
039        private static final Logger ourLog = LoggerFactory.getLogger(MailSvc.class);
040
041        private final MailConfig myMailConfig;
042        private final Mailer myMailer;
043
044        public MailSvc(@Nonnull MailConfig theMailConfig) {
045                Validate.notNull(theMailConfig);
046                myMailConfig = theMailConfig;
047                myMailer = makeMailer(myMailConfig);
048        }
049
050        @Override
051        public void sendMail(@Nonnull List<Email> theEmails) {
052                Validate.notNull(theEmails);
053                theEmails.forEach(theEmail -> send(theEmail, new OnSuccess(theEmail), new ErrorHandler(theEmail)));
054        }
055
056        @Override
057        public void sendMail(@Nonnull Email theEmail) {
058                send(theEmail, new OnSuccess(theEmail), new ErrorHandler(theEmail));
059        }
060
061        @Override
062        public void sendMail(
063                        @Nonnull Email theEmail, @Nonnull Runnable theOnSuccess, @Nonnull ExceptionConsumer theErrorHandler) {
064                send(theEmail, theOnSuccess, theErrorHandler);
065        }
066
067        private void send(
068                        @Nonnull Email theEmail, @Nonnull Runnable theOnSuccess, @Nonnull ExceptionConsumer theErrorHandler) {
069                Validate.notNull(theEmail);
070                Validate.notNull(theOnSuccess);
071                Validate.notNull(theErrorHandler);
072                try {
073                        final AsyncResponse asyncResponse = myMailer.sendMail(theEmail, true);
074                        if (asyncResponse != null) {
075                                asyncResponse.onSuccess(theOnSuccess);
076                                asyncResponse.onException(theErrorHandler);
077                        }
078                } catch (MailException e) {
079                        theErrorHandler.accept(e);
080                }
081        }
082
083        @Nonnull
084        private Mailer makeMailer(@Nonnull MailConfig theMailConfig) {
085                ourLog.info(
086                                "SMTP Mailer config Hostname:[{}] | Port:[{}] | Username:[{}] | TLS:[{}]",
087                                theMailConfig.getSmtpHostname(),
088                                theMailConfig.getSmtpPort(),
089                                theMailConfig.getSmtpUsername(),
090                                theMailConfig.isSmtpUseStartTLS());
091                return MailerBuilder.withSMTPServer(
092                                                theMailConfig.getSmtpHostname(),
093                                                theMailConfig.getSmtpPort(),
094                                                theMailConfig.getSmtpUsername(),
095                                                theMailConfig.getSmtpPassword())
096                                .withTransportStrategy(
097                                                theMailConfig.isSmtpUseStartTLS() ? TransportStrategy.SMTP_TLS : TransportStrategy.SMTP)
098                                .buildMailer();
099        }
100
101        @Nonnull
102        private String makeMessage(@Nonnull Email theEmail) {
103                return " with subject [" + theEmail.getSubject() + "] and recipients ["
104                                + theEmail.getRecipients().stream().map(Recipient::getAddress).collect(Collectors.joining(",")) + "]";
105        }
106
107        private class OnSuccess implements Runnable {
108                private final Email myEmail;
109
110                private OnSuccess(@Nonnull Email theEmail) {
111                        myEmail = theEmail;
112                }
113
114                @Override
115                public void run() {
116                        ourLog.info("Email sent" + makeMessage(myEmail));
117                }
118        }
119
120        private class ErrorHandler implements ExceptionConsumer {
121                private final Email myEmail;
122
123                private ErrorHandler(@Nonnull Email theEmail) {
124                        myEmail = theEmail;
125                }
126
127                @Override
128                public void accept(Exception t) {
129                        ourLog.error("Email not sent" + makeMessage(myEmail), t);
130                }
131        }
132}