001package ca.uhn.fhir.rest.server.mail;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
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 javax.annotation.Nonnull;
036import java.util.List;
037import java.util.stream.Collectors;
038
039public class MailSvc implements IMailSvc {
040        private static final Logger ourLog = LoggerFactory.getLogger(MailSvc.class);
041
042        private final MailConfig myMailConfig;
043        private final Mailer myMailer;
044
045        public MailSvc(@Nonnull MailConfig theMailConfig) {
046                Validate.notNull(theMailConfig);
047                myMailConfig = theMailConfig;
048                myMailer = makeMailer(myMailConfig);
049        }
050
051        @Override
052        public void sendMail(@Nonnull List<Email> theEmails) {
053                Validate.notNull(theEmails);
054                theEmails.forEach(theEmail -> send(theEmail, new OnSuccess(theEmail), new ErrorHandler(theEmail)));
055        }
056
057        @Override
058        public void sendMail(@Nonnull Email theEmail) {
059                send(theEmail, new OnSuccess(theEmail), new ErrorHandler(theEmail));
060        }
061
062        @Override
063        public void sendMail(@Nonnull Email theEmail,
064                                                                @Nonnull Runnable theOnSuccess,
065                                                                @Nonnull ExceptionConsumer theErrorHandler) {
066                send(theEmail, theOnSuccess, theErrorHandler);
067        }
068
069        private void send(@Nonnull Email theEmail,
070                                                        @Nonnull Runnable theOnSuccess,
071                                                        @Nonnull ExceptionConsumer theErrorHandler) {
072                Validate.notNull(theEmail);
073                Validate.notNull(theOnSuccess);
074                Validate.notNull(theErrorHandler);
075                try {
076                        final AsyncResponse asyncResponse = myMailer.sendMail(theEmail, true);
077                        if (asyncResponse != null) {
078                                asyncResponse.onSuccess(theOnSuccess);
079                                asyncResponse.onException(theErrorHandler);
080                        }
081                } catch (MailException e) {
082                        theErrorHandler.accept(e);
083                }
084        }
085
086        @Nonnull
087        private Mailer makeMailer(@Nonnull MailConfig theMailConfig) {
088                ourLog.info("SMTP Mailer config Hostname:[{}] | Port:[{}] | Username:[{}] | TLS:[{}]",
089                        theMailConfig.getSmtpHostname(), theMailConfig.getSmtpPort(),
090                        theMailConfig.getSmtpUsername(), theMailConfig.isSmtpUseStartTLS());
091                return MailerBuilder
092                        .withSMTPServer(
093                                theMailConfig.getSmtpHostname(),
094                                theMailConfig.getSmtpPort(),
095                                theMailConfig.getSmtpUsername(),
096                                theMailConfig.getSmtpPassword())
097                        .withTransportStrategy(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}