Commit e6ea475d authored by Deep Dey's avatar Deep Dey

feat: add mail integration with template engine

parent c7350bd2
......@@ -145,6 +145,14 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -2,13 +2,16 @@ package com.example.LaMartin.admission.dao.controller;
import com.example.LaMartin.admission.dao.repository.AdmissionFormDataRepository;
import com.example.LaMartin.admission.model.AdmissionFormData;
import com.example.LaMartin.common.service.EmailService;
import com.example.LaMartin.common.service.FileStorageService;
import com.example.LaMartin.common.service.TemplateService;
import com.example.LaMartin.config.Settings;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.mail.MessagingException;
import java.io.IOException;
import java.util.List;
......@@ -20,7 +23,10 @@ public class AdmissionFormDAOController {
private final FileStorageService fileStorageService;
private final AdmissionFormDataRepository admissionFormDataRepository;
public boolean addNewAdmissionData(@NotNull AdmissionFormData formData) throws IOException {
private final EmailService emailService;
private final TemplateService templateService;
public boolean addNewAdmissionData(@NotNull AdmissionFormData formData) throws IOException, MessagingException {
formData.setCandidateImageName(
fileStorageService.storeFile(
......@@ -43,7 +49,21 @@ public class AdmissionFormDAOController {
)
);
return admissionFormDataRepository.saveAdmissionFormData(formData) > 0;
Long id = admissionFormDataRepository.saveAdmissionFormData(formData);
formData.setId(id);
if (id > 0) {
String emailBody = this.templateService
.generateAutoReplyTemplate(formData);
this.emailService.sendHtmlEmail(
formData.getFatherEmail(),
"AUTO REPLY | " + formData.getSchool().toUpperCase(),
emailBody
);
}
return id > 0;
}
public List<AdmissionFormData> getAdmissionFormDataList(
......@@ -68,9 +88,23 @@ public class AdmissionFormDAOController {
Long id,
String interactionDate,
String interactionTime
) {
return admissionFormDataRepository
) throws Exception {
boolean updated = admissionFormDataRepository
.updateInteractionDateTime(id, interactionDate, interactionTime);
if (updated) {
AdmissionFormData data = this.admissionFormDataRepository.getAdmissionFormDataById(id);
String emailBody = this.templateService
.generateAdmissionInterviewTemplate(data);
this.emailService.sendHtmlEmail(
data.getFatherEmail(),
"SCHEDULED INTERVIEW | " + data.getSchool().toUpperCase(),
emailBody
);
}
return updated;
}
}
......@@ -153,6 +153,17 @@ public class AdmissionFormDataRepository {
return namedParameterJdbcTemplate.query(sql, parameters, new AdmissionFormMapper());
}
public AdmissionFormData getAdmissionFormDataById(Long formId) {
String sql = "SELECT * FROM admission_form_data WHERE id = :id";
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("id", formId);
return namedParameterJdbcTemplate
.queryForObject(sql, parameters, new AdmissionFormMapper());
}
public boolean updateInteractionDateTime(
Long admissionFormId,
String interactionDate,
......
package com.example.LaMartin.common.service;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service
@AllArgsConstructor(onConstructor_ = @__({@Autowired}))
public class EmailService {
private final JavaMailSender mailSender;
public void sendHtmlEmail(String to, String subject, String htmlBody) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setSubject(subject);
helper.setTo(to);
helper.setText(htmlBody, true); // true indicates HTML
mailSender.send(message);
}
}
package com.example.LaMartin.common.service;
import com.example.LaMartin.admission.model.AdmissionFormData;
import com.example.LaMartin.config.Settings;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -10,15 +11,33 @@ import org.thymeleaf.context.Context;
import java.util.List;
@Service
@AllArgsConstructor(onConstructor_ = @__({@Autowired}) )
@AllArgsConstructor(onConstructor_ = @__({@Autowired}))
public class TemplateService {
private final TemplateEngine templateEngine;
private final Settings settings;
public String generateAdmissionFormListTemplate(List<AdmissionFormData> admissionFormDataList) {
Context context = new Context();
context.setVariable("admissionFormList", admissionFormDataList);
context.setVariable("serverPath", settings.getStaticServerPath());
return templateEngine.process("AdmissionFormListTemplate", context);
}
public String generateAdmissionInterviewTemplate(AdmissionFormData admissionFormData) {
Context context = new Context();
context.setVariable("admissionForm", admissionFormData);
context.setVariable("serverPath", settings.getStaticServerPath());
return templateEngine.process("AdmissionInterviewTemplate", context);
}
public String generateAutoReplyTemplate(AdmissionFormData admissionFormData) {
Context context = new Context();
context.setVariable("admissionForm", admissionFormData);
context.setVariable("serverPath", settings.getStaticServerPath());
return templateEngine.process("FormAutoReplyTemplate", context);
}
}
package com.example.LaMartin.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
@Configuration
public class EmailConfig {
@Value("${spring.mail.host}")
private String emailHost;
@Value("${spring.mail.port}")
private int emailPort;
@Value("${spring.mail.username}")
private String emailUsername;
@Value("${spring.mail.password}")
private String emailPassword;
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(emailHost);
mailSender.setPort(emailPort);
mailSender.setUsername(emailUsername);
mailSender.setPassword(emailPassword);
Properties properties = new Properties();
// set the below two lines as per the SMTP server requirement
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
mailSender.setJavaMailProperties(properties);
return mailSender;
}
}
......@@ -11,6 +11,9 @@ public class Settings {
@Value("${lamartin.static.resources.root.path}") // Read path from application.properties
private String staticResourcesRootPath;
@Value("${lamartin.static.server.path}")
private String staticServerPath;
public String getImageStoragePath() {
return staticResourcesRootPath + "/images/";
}
......
......@@ -26,6 +26,9 @@ public class WebMvcConfig implements WebMvcConfigurer {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("file:" + settings.getStaticResourcesRootPath() + "/");
registry.addResourceHandler("/static/**").
addResourceLocations("classpath:/static/");
}
@Override
......
......@@ -8,8 +8,8 @@ spring.datasource.username=postgres
spring.datasource.password=2580
spring.redis.host=localhost
spring.redis.port=6379
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
lamartin.static.resources.root.path=D:/WEBSOFTTECHS/webomagic/data
\ No newline at end of file
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Intimation for Interaction</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
width: 800px;
margin: 0 auto;
padding: 20px;
box-sizing: border-box;
}
.header {
display: flex;
align-items: center;
border-bottom: 3px solid #204C89;
text-align: left;
padding: 10px 5px;
margin-bottom: 10px;
background: #F1C232;
}
.header img {
width: 100px;
margin-right: 20px;
}
.header .title {
color: #204C89;
font-weight: bold;
font-size: 18px;
}
.main-section {
border: 2px solid #F1C232;
padding: 10px 10px 5px;
margin-bottom: 10px;
}
.main-sub-section {
display: flex;
justify-content: space-between;
align-items: start;
}
.details {
width: 50%;
margin-bottom: 20px;
}
.details p {
margin: 3px 0;
font-size: 14px;
}
.interaction {
width: 50%;
border: 1px solid #353535;
margin-bottom: 20px;
}
.interaction div {
text-align: center;
flex: 1;
}
.interaction-date {
display: flex;
justify-content: center;
align-items: center;
border-bottom: 1px solid #353535;
padding: 10px 5px;
}
.interaction-images {
display: flex;
justify-content: center;
align-items: center;
padding: 10px 5px;
font-size: 14px;
font-weight: bold;
}
.interaction-images img {
width: 100px;
margin: 8px 0;
}
.note {
font-weight: bold;
margin: 5px;
font-size: 12px;
}
.documents {
margin-bottom: 20px;
}
.documents ul {
list-style: none;
padding: 0;
font-size: 13px;
}
.documents li {
margin: 5px 0;
}
.footer {
padding-top: 10px;
margin-top: 20px;
font-size: 13px;
}
.footer p {
text-align: center;
margin-top: 15px;
background-color: #F1C232;
padding: 5px;
font-weight: bold;
font-size: 14px;
color: white;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<img th:src="@{${serverPath + 'static/la-martin-logo.webp'}}" alt="Logo">
<div class="title">
<div>INTIMATION FOR INTERACTION</div>
<div style="text-transform: uppercase;" th:text="${admissionForm.school}"></div>
</div>
</div>
<div class="main-section">
<div class="main-sub-section">
<div class="details">
<p><strong>Candidate: <span th:text="${admissionForm.fullName}"></span></strong></p>
<p><strong>Father: <span th:text="${admissionForm.fatherName}"></span></strong></p>
<p><strong>Mother: <span th:text="${admissionForm.motherName}"></span></strong></p>
<p><strong>Father's Email: <span th:text="${admissionForm.fatherEmail}"></span></strong></p>
<p><strong>Father's Mobile: <span th:text="${admissionForm.fatherMobile}"></span></strong></p>
<br>
<p><strong>Your date and time of Interaction</strong></p>
<p>Interaction Date: <strong th:text="${admissionForm.interactionDate}"></strong></p>
<p>Interaction Time: <strong th:text="${admissionForm.interactionTime}">10:00 AM</strong></p>
</div>
<div class="interaction" style="flex-direction: column;">
<div class="interaction-date">
<div>
<div>Interaction Date:</div>
<div><strong th:text="${admissionForm.interactionDate}">2024-01-31</strong></div>
</div>
<div>
<div>Interaction Time:</div>
<div><strong th:text="${admissionForm.interactionTime}">10:00 AM</strong></div>
</div>
</div>
<div class="interaction-images">
<div>
<img th:src="@{${serverPath + 'resources/images/' + admissionForm.candidateImageName}}" alt="Candidate">
<div><i>Candidate</i></div>
</div>
<div>
<img th:src="@{${serverPath + 'resources/images/' + admissionForm.fatherImageName}}" alt="Father">
<div><i>Father</i></div>
</div>
<div>
<img th:src="@{${serverPath + 'resources/images/' + admissionForm.motherImageName}}" alt="Mother">
<div><i>Mother</i></div>
</div>
</div>
<div class="note">
<div>BOTH PARENTS AND CANDIDATE MUST ATTEND THE INTERACTION. DRESS CODE: FORMAL</div>
<div style="margin-top: 5px;">CALL FOR INTERACTION IS NO GUARANTEE OF ADMISSION DATE AND TIME OF THE INTERACTION CANNOT BE CHANGED</div>
</div>
</div>
</div>
<div class="documents">
<p style="text-align: center; font-weight: bold; padding: 15px 100px;">YOU MUST BRING IN ORIGINAL ALONG WITH A PHOTOCOPY (SELF-ATTESTED) OF THE FOLLOWING DOCUMENTS IN SUPPORT OF YOUR DECLARATION GIVEN.</p>
<ul>
<li>1. Municipal Corporation Birth Certificate</li>
<li>2. Hospital/Nursing Home Discharge Certificate</li>
<li>3. Baptism Certificate for Christian candidates (if applicable)</li>
<li>4. School Leaving Certificate of Ex-Students</li>
<li>5. Prescribed Medical Certificate (in original) duly filled in by Qualified Medical Practitioner<br>&nbsp;&nbsp;&nbsp;<b>(format available on our website https://lamartiniere.co)</b></li>
<li>6. Proof of Employment/ Business of Parent</li>
<li>7. Proof of Educational & Professional Qualification for both parents.</li>
<li>8. Proof of income of both the parents.</li>
<li>9. Legal Adoption papers, if applicable.</li>
<li>10. In case of Single Parent, Decree of the Court regarding the custody of the child.</li>
</ul>
</div>
<div class="footer">
<img th:src="@{${serverPath + 'static/signature.png'}}" width="100px">
<div><strong>SUPRIYO DHAR</strong></div>
<div>SECRETARY, LA MARTINIERE, KOLKATA</div>
<p>A PRINTOUT OF THIS PAGE (IN ORIGINAL) MUST BE BROUGHT DURING THE INTERACTION AND SHOWN TO THE SECURITY PERSONNEL AT THE SCHOOL GATE</p>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Title</title>
<title>La Martiniere for Girls - Proof of Delivery</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 50px auto;
padding: 20px;
border-radius: 10px;
}
.header {
text-align: center;
}
.header h1 {
margin-bottom: 5px;
}
.proof-of-delivery {
text-align: center;
margin: 20px 0;
padding: 10px;
background-color: #d9edf7;
color: #31708f;
border: 1px solid #bce8f1;
border-radius: 5px;
}
.email {
color: green;
}
.details {
margin: 20px 0;
}
.details p {
margin: 5px 0;
}
.details {
font-weight: bold;
}
.details span {
font-weight: normal;
}
.footer {
margin: 20px 0;
text-align: center;
font-size: 12px;
color: #555;
}
.social-links a {
margin: 0 10px;
text-decoration: none;
color: #007bff;
}
.social-links a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1 th:text="${title}">Title</h1>
<p th:text="${content}">Content goes here...</p>
<div class="container">
<div class="header">
<img th:src="@{${serverPath + 'static/la-martin-logo.webp'}}" alt="Proof of Delivery" height="80px">
<h2 style="text-transform: capitalize;" th:text="${admissionForm.school}"></h2>
</div>
<div class="proof-of-delivery">
<p>Thank you. Your application has been received.</p>
</div>
<div class="details">
<p>CATEGORY: <span th:text="${admissionForm.category}"></span></p>
<p>CLASS: <span th:text="${admissionForm.admissionClass}"></span></p>
<p>NAME OF CANDIDATE: <span th:text="${admissionForm.fullName}"></span></p>
<p>DATE OF BIRTH: <span th:text="${admissionForm.dateOfBirth}"></span></p>
<p>FATHER'S NAME: <span th:text="${admissionForm.fatherName}"></span></p>
<p>MOTHER'S NAME: <span th:text="${admissionForm.motherName}"></span></p>
</div>
<div class="footer">
<p>Please quote the reference <span class="email">piklughosh01@gmail.com</span> for further correspondence until such time the admission procedure is under way.</p>
</div>
</div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment