001/*-
002 * #%L
003 * HAPI FHIR Subscription Server
004 * %%
005 * Copyright (C) 2014 - 2023 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.jpa.subscription.match.deliver.email;
021
022import ca.uhn.fhir.rest.server.mail.IMailSvc;
023import ca.uhn.fhir.util.StopWatch;
024import org.apache.commons.lang3.Validate;
025import org.simplejavamail.api.email.Email;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import javax.annotation.Nonnull;
030
031public class EmailSenderImpl implements IEmailSender {
032
033        private static final Logger ourLog = LoggerFactory.getLogger(EmailSenderImpl.class);
034
035        private final IMailSvc myMailSvc;
036
037        public EmailSenderImpl(@Nonnull IMailSvc theMailSvc) {
038                Validate.notNull(theMailSvc);
039                myMailSvc = theMailSvc;
040        }
041
042        @Override
043        public void send(EmailDetails theDetails) {
044                StopWatch stopWatch = new StopWatch();
045
046                ourLog.info(
047                                "Sending email for subscription {} from [{}] to recipients: [{}]",
048                                theDetails.getSubscriptionId(),
049                                theDetails.getFrom(),
050                                theDetails.getTo());
051
052                Email email = theDetails.toEmail();
053
054                myMailSvc.sendMail(
055                                email,
056                                () -> ourLog.info(
057                                                "Done sending email for subscription {} from [{}] to recipients: [{}] (took {}ms)",
058                                                theDetails.getSubscriptionId(),
059                                                theDetails.getFrom(),
060                                                theDetails.getTo(),
061                                                stopWatch.getMillis()),
062                                (e) -> {
063                                        ourLog.error(
064                                                        "Error sending email for subscription {} from [{}] to recipients: [{}] (took {}ms)",
065                                                        theDetails.getSubscriptionId(),
066                                                        theDetails.getFrom(),
067                                                        theDetails.getTo(),
068                                                        stopWatch.getMillis());
069                                        ourLog.error("Error sending email", e);
070                                });
071        }
072}