From ee5413715ba51d10a41ac2b785f0030fc83adf5f Mon Sep 17 00:00:00 2001 From: Michal Badin <xbadin@fi.muni.cz> Date: Mon, 8 May 2023 20:11:14 +0200 Subject: [PATCH] fix(Accepted-email): Email is sent --- .../java/cz/muni/pa165/config/MailConfig.java | 4 ++- .../pa165/service/NotificationService.java | 34 +++++++++--------- .../templates/{mails => }/accepted-email.html | 36 ++++++++++++------- 3 files changed, 43 insertions(+), 31 deletions(-) rename notification/src/main/resources/templates/{mails => }/accepted-email.html (76%) diff --git a/notification/src/main/java/cz/muni/pa165/config/MailConfig.java b/notification/src/main/java/cz/muni/pa165/config/MailConfig.java index 7c00860..1faf9d0 100644 --- a/notification/src/main/java/cz/muni/pa165/config/MailConfig.java +++ b/notification/src/main/java/cz/muni/pa165/config/MailConfig.java @@ -11,6 +11,7 @@ import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; import org.thymeleaf.templateresolver.ITemplateResolver; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Properties; @@ -43,8 +44,9 @@ public class MailConfig { private ITemplateResolver htmlTemplateResolver() { final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); - templateResolver.setPrefix("templates/mails/"); + templateResolver.setPrefix("/templates/"); templateResolver.setSuffix(".html"); + templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name()); templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setCacheable(false); return templateResolver; diff --git a/notification/src/main/java/cz/muni/pa165/service/NotificationService.java b/notification/src/main/java/cz/muni/pa165/service/NotificationService.java index c96fac5..c7f73c9 100644 --- a/notification/src/main/java/cz/muni/pa165/service/NotificationService.java +++ b/notification/src/main/java/cz/muni/pa165/service/NotificationService.java @@ -6,15 +6,16 @@ import jakarta.mail.internet.MimeMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.List; @Service @@ -32,7 +33,7 @@ public class NotificationService { } public ConfirmationDto notify(NotificationDto notificationDto) { - return sendEmail(notificationDto.getSubject(), notificationDto.getMessage(), notificationDto.getMessage(), notificationDto.getReceivers()); + return sendEmail(notificationDto.getSubject(), notificationDto.getMessage(), notificationDto.getReceivers()); } public ConfirmationDto notifyNewComponent(CarComponentNotificationDto carComponentNotificationDto) { @@ -43,7 +44,7 @@ public class NotificationService { + "\nInformation: " + c.getInformation() + "\nWeight: " + c.getWeight(); - return sendEmail(subject, text, text, carComponentNotificationDto.getReceivers()); + return sendEmail(subject, text, carComponentNotificationDto.getReceivers()); } public ConfirmationDto notifyNewApplication(ApplicationNotificationDto applicationNotificationDto) { @@ -58,24 +59,23 @@ public class NotificationService { + "\nFilling out date: " + a.getFillingOutDate(); - return sendEmail(subject, text, text, applicationNotificationDto.getReceivers()); + return sendEmail(subject, text, applicationNotificationDto.getReceivers()); } public ConfirmationDto notifyApplicationStatusChange(ApplicationNotificationDto applicationNotificationDto) { var a = applicationNotificationDto.getApplication(); var subject = "Application for Formula Driver Position"; - var dtoText = ""; var htmlContent = ""; - if (a.getStatus() == ApplicationStatus.ACCEPTED) { - Context ctx = new Context(); - ctx.setVariable("fullName", getFullName(a)); - ctx.setVariable("today", new SimpleDateFormat("dd/MM/yyyy").format(LocalDate.now())); + Context ctx = new Context(); + ctx.setVariable("fullName", getFullName(a)); + ctx.setVariable("today", LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))); - htmlContent = templateEngine.process("accepted-email.html", ctx); - dtoText = "Application has been accepted"; + if (a.getStatus() == ApplicationStatus.ACCEPTED) { + htmlContent = templateEngine.process("accepted-email", ctx); } else { - dtoText = "Dear " + getFullName(a) + ",\n" + + htmlContent = templateEngine.process("rejected-email", ctx); + htmlContent = "Dear " + getFullName(a) + ",\n" + "\n" + "I regret to inform you that your application for the Formula Driver position has been declined. While we appreciate your interest in the role, we have decided to pursue other candidates whose skills and experience more closely match our requirements.\n" + "\n" + @@ -90,18 +90,18 @@ public class NotificationService { "Formula Team Management"; } - return sendEmail(subject, htmlContent, dtoText, applicationNotificationDto.getReceivers()); + return sendEmail(subject, htmlContent, applicationNotificationDto.getReceivers()); } - private ConfirmationDto sendEmail(String subject, String htmlContent, String dtoContent, List<String> receivers) { + private ConfirmationDto sendEmail(String subject, String text, List<String> receivers) { MimeMessage message = emailSender.createMimeMessage(); try { - MimeMessageHelper helper = new MimeMessageHelper(message, true); + MimeMessageHelper helper = new MimeMessageHelper(message, true, StandardCharsets.UTF_8.name()); helper.setFrom(EMAIL); helper.setTo(receivers.toArray(String[]::new)); helper.setSubject(subject); - helper.setText(htmlContent, true); + helper.setText(text, true); emailSender.send(message); @@ -109,7 +109,7 @@ public class NotificationService { .sender(EMAIL) .receivers(receivers) .subject(subject) - .message(dtoContent); + .message(text); } catch (MessagingException e) { throw new RuntimeException("Failed to send email", e); } diff --git a/notification/src/main/resources/templates/mails/accepted-email.html b/notification/src/main/resources/templates/accepted-email.html similarity index 76% rename from notification/src/main/resources/templates/mails/accepted-email.html rename to notification/src/main/resources/templates/accepted-email.html index b223448..4f41dcb 100644 --- a/notification/src/main/resources/templates/mails/accepted-email.html +++ b/notification/src/main/resources/templates/accepted-email.html @@ -1,5 +1,5 @@ <!DOCTYPE html> -<html xmlns:th="http://www.thymeleaf.org"> +<html xmlns:th="http://www.thymeleaf.org" lang="en"> <head> <meta charset="utf-8"> <title>Application for Formula Driver Position</title> @@ -11,8 +11,19 @@ background-color: #f6f6f6; } + .header { + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + } + + .header-right { + margin-left: auto; + } + .container { - max-width: 600px; + max-width: 625px; margin: 0 auto; background-color: #fff; padding: 20px; @@ -31,18 +42,12 @@ font-size: 14px; font-weight: normal; margin: 0; - position: absolute; - top: 10px; - right: 20px; } .location { font-size: 14px; font-weight: normal; margin: 0; - position: absolute; - top: 30px; - right: 20px; } p { @@ -62,16 +67,21 @@ </head> <body> <div class="container"> - <h1>Application for Formula Driver Position</h1> + <div class="header"> + <h1>Application for Formula Driver Position</h1> + <div class="header-right"> + <div class="date" th:text="${today}"></div> + <div class="location">Brno, Czech Republic</div> + </div> + </div> + <p><b>Dear <span th:text="${fullName}"></span>,</b></p> <p>I am pleased to inform you that your application for the position of Formula Driver has been <b>Accepted</b>. We are excited to have you join our team and look forward to working with you.</p> <p>Your experience, skills, and passion for racing make you an excellent fit for our team. We believe that you will be a valuable asset to our organization and we are eager to see what you can accomplish on the track.</p> <p>As a next step, we will contact you shortly to discuss the details of your employment contract and to schedule your training and orientation. In the meantime, please do not hesitate to contact us if you have any questions or concerns.</p> <p>Thank you for your interest in our team and for taking the time to apply. We look forward to a long and successful partnership with you.</p> - <p>Best regards,</p> - <p>Formula Team Management</p> - <div class="date" th:text="${today}"></div> - <div class="location">Brno, Czech Republic</div> + <p>Best regards, + <br>Formula Team Management</p> <div class="signature">-- Formula Team Management</div> </div> </body> -- GitLab