Skip to content
Snippets Groups Projects
Commit ee541371 authored by Michal Badin's avatar Michal Badin
Browse files

fix(Accepted-email): Email is sent

parent 272f6974
No related branches found
No related tags found
2 merge requests!54Merge develop into main,!51Thymeleaf
...@@ -11,6 +11,7 @@ import org.thymeleaf.templatemode.TemplateMode; ...@@ -11,6 +11,7 @@ import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver; import org.thymeleaf.templateresolver.ITemplateResolver;
import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.Properties; import java.util.Properties;
...@@ -43,8 +44,9 @@ public class MailConfig { ...@@ -43,8 +44,9 @@ public class MailConfig {
private ITemplateResolver htmlTemplateResolver() { private ITemplateResolver htmlTemplateResolver() {
final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/mails/"); templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html"); templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false); templateResolver.setCacheable(false);
return templateResolver; return templateResolver;
......
...@@ -6,15 +6,16 @@ import jakarta.mail.internet.MimeMessage; ...@@ -6,15 +6,16 @@ import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine; import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context; import org.thymeleaf.context.Context;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
@Service @Service
...@@ -32,7 +33,7 @@ public class NotificationService { ...@@ -32,7 +33,7 @@ public class NotificationService {
} }
public ConfirmationDto notify(NotificationDto notificationDto) { 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) { public ConfirmationDto notifyNewComponent(CarComponentNotificationDto carComponentNotificationDto) {
...@@ -43,7 +44,7 @@ public class NotificationService { ...@@ -43,7 +44,7 @@ public class NotificationService {
+ "\nInformation: " + c.getInformation() + "\nInformation: " + c.getInformation()
+ "\nWeight: " + c.getWeight(); + "\nWeight: " + c.getWeight();
return sendEmail(subject, text, text, carComponentNotificationDto.getReceivers()); return sendEmail(subject, text, carComponentNotificationDto.getReceivers());
} }
public ConfirmationDto notifyNewApplication(ApplicationNotificationDto applicationNotificationDto) { public ConfirmationDto notifyNewApplication(ApplicationNotificationDto applicationNotificationDto) {
...@@ -58,24 +59,23 @@ public class NotificationService { ...@@ -58,24 +59,23 @@ public class NotificationService {
+ "\nFilling out date: " + a.getFillingOutDate(); + "\nFilling out date: " + a.getFillingOutDate();
return sendEmail(subject, text, text, applicationNotificationDto.getReceivers()); return sendEmail(subject, text, applicationNotificationDto.getReceivers());
} }
public ConfirmationDto notifyApplicationStatusChange(ApplicationNotificationDto applicationNotificationDto) { public ConfirmationDto notifyApplicationStatusChange(ApplicationNotificationDto applicationNotificationDto) {
var a = applicationNotificationDto.getApplication(); var a = applicationNotificationDto.getApplication();
var subject = "Application for Formula Driver Position"; var subject = "Application for Formula Driver Position";
var dtoText = "";
var htmlContent = ""; var htmlContent = "";
if (a.getStatus() == ApplicationStatus.ACCEPTED) {
Context ctx = new Context(); Context ctx = new Context();
ctx.setVariable("fullName", getFullName(a)); ctx.setVariable("fullName", getFullName(a));
ctx.setVariable("today", new SimpleDateFormat("dd/MM/yyyy").format(LocalDate.now())); ctx.setVariable("today", LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));
htmlContent = templateEngine.process("accepted-email.html", ctx); if (a.getStatus() == ApplicationStatus.ACCEPTED) {
dtoText = "Application has been accepted"; htmlContent = templateEngine.process("accepted-email", ctx);
} else { } else {
dtoText = "Dear " + getFullName(a) + ",\n" + htmlContent = templateEngine.process("rejected-email", ctx);
htmlContent = "Dear " + getFullName(a) + ",\n" +
"\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" + "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" + "\n" +
...@@ -90,18 +90,18 @@ public class NotificationService { ...@@ -90,18 +90,18 @@ public class NotificationService {
"Formula Team Management"; "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(); MimeMessage message = emailSender.createMimeMessage();
try { try {
MimeMessageHelper helper = new MimeMessageHelper(message, true); MimeMessageHelper helper = new MimeMessageHelper(message, true, StandardCharsets.UTF_8.name());
helper.setFrom(EMAIL); helper.setFrom(EMAIL);
helper.setTo(receivers.toArray(String[]::new)); helper.setTo(receivers.toArray(String[]::new));
helper.setSubject(subject); helper.setSubject(subject);
helper.setText(htmlContent, true); helper.setText(text, true);
emailSender.send(message); emailSender.send(message);
...@@ -109,7 +109,7 @@ public class NotificationService { ...@@ -109,7 +109,7 @@ public class NotificationService {
.sender(EMAIL) .sender(EMAIL)
.receivers(receivers) .receivers(receivers)
.subject(subject) .subject(subject)
.message(dtoContent); .message(text);
} catch (MessagingException e) { } catch (MessagingException e) {
throw new RuntimeException("Failed to send email", e); throw new RuntimeException("Failed to send email", e);
} }
......
<!DOCTYPE html> <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <html xmlns:th="http://www.thymeleaf.org" lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Application for Formula Driver Position</title> <title>Application for Formula Driver Position</title>
...@@ -11,8 +11,19 @@ ...@@ -11,8 +11,19 @@
background-color: #f6f6f6; background-color: #f6f6f6;
} }
.header {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.header-right {
margin-left: auto;
}
.container { .container {
max-width: 600px; max-width: 625px;
margin: 0 auto; margin: 0 auto;
background-color: #fff; background-color: #fff;
padding: 20px; padding: 20px;
...@@ -31,18 +42,12 @@ ...@@ -31,18 +42,12 @@
font-size: 14px; font-size: 14px;
font-weight: normal; font-weight: normal;
margin: 0; margin: 0;
position: absolute;
top: 10px;
right: 20px;
} }
.location { .location {
font-size: 14px; font-size: 14px;
font-weight: normal; font-weight: normal;
margin: 0; margin: 0;
position: absolute;
top: 30px;
right: 20px;
} }
p { p {
...@@ -62,16 +67,21 @@ ...@@ -62,16 +67,21 @@
</head> </head>
<body> <body>
<div class="container"> <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><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>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>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>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>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>Best regards,
<p>Formula Team Management</p> <br>Formula Team Management</p>
<div class="date" th:text="${today}"></div>
<div class="location">Brno, Czech Republic</div>
<div class="signature">-- Formula Team Management</div> <div class="signature">-- Formula Team Management</div>
</div> </div>
</body> </body>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment