Skip to content

Commit

Permalink
Merge pull request #23 from rajumb0232/dev
Browse files Browse the repository at this point in the history
refactoring the OTP verification mail and confirmation mail code to use Thymeleaf templates
  • Loading branch information
rajumb0232 authored Nov 9, 2024
2 parents b2b7a3a + f552042 commit 4c36ed7
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 36 deletions.
4 changes: 4 additions & 0 deletions E-Stores-API/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- MAILING SERVICE DEPENDENCIES -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,34 +268,7 @@ public void revokeAllTokens() {
}

private void sendOTPToMailId(User user, int otp) throws MessagingException {
String text =
"<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
" <title>Mail</title>\n" +
"</head>\n" +
"<style>\n" +
" *{\n" +
" font-family: Verdana, Geneva, Tahoma, sans-serif\n" +
" }\n" +
" h4 {\n" +
" color: rgb(0, 98, 255);\n" +
" }\n" +
"</style>\n" +
"<body>\n" +
" <div>\n" +
" <h3>Hurrey!!! You are just few steps away!</h3>\n" +
" <p>Please use this OTP given below for further verification.</p>\n" +
" <h4>" + otp + "</h4>\n" +
" <br> <br> <br>\n" +
" <p>with best regards</p>\n" +
" <p>E Stores</p>\n" +
" </div>\n" +
"</body>\n" +
"</html>";

String text = mailService.createHtmlFor("otp-verification-request", Map.of("otp", otp));
mailService.sendMail(MessageData.builder()
.to(user.getEmail())
.subject("Verify your email for E Stores")
Expand All @@ -306,15 +279,10 @@ private void sendOTPToMailId(User user, int otp) throws MessagingException {
private void sendConfirmationMail(User user) throws MessagingException {
mailService.sendMail(MessageData.builder()
.to(user.getEmail())
.subject("Welcome to Flipkart family")
.subject("Welcome to E Stores family")
.sentDate(new Date())
.text(
"<b>Congratulations, your now a part of flipkart family, your email verification is " +
"successfully completed</b>" +
"<br><br>" +
"With Best Regards,<br>" +
"Flipkart"
)
.text(mailService.createHtmlFor("simple-message",
Map.of("message", "Congratulations, your now a part of E Stores family, your email verification is successfully completed")))
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.Map;

@Service
@AllArgsConstructor
public class MailService {

private final JavaMailSender javaMailSender;
private final TemplateEngine templateEngine;

@Async
public void sendMail(MessageData messageData) throws MessagingException {
Expand All @@ -26,4 +31,10 @@ public void sendMail(MessageData messageData) throws MessagingException {
helper.setText(messageData.getText(), true);
javaMailSender.send(message);
}

public String createHtmlFor(String template, Map<String, Object> variables) {
Context context = new Context();
context.setVariables(variables);
return templateEngine.process(template, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Verification OTP</title>
</head>
<body style="margin: 0; padding: 0; background-color: #f7f7f7; font-family: Arial, sans-serif; -webkit-font-smoothing: antialiased;">
<!-- Main Table -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="background-color: #f7f7f7; padding: 20px; min-width: 100%; table-layout: fixed;">
<tr>
<td align="center" valign="top">
<!-- Content Table -->
<table border="0" cellpadding="0" cellspacing="0" style="background-color: #ffffff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); max-width: 600px; width: 100%;">
<!-- Logo/Brand Section -->
<tr>
<td align="center" style="padding: 40px 40px 20px 40px;">
<h1 style="margin: 0; font-size: 28px; color: #f9a825; font-weight: bold; letter-spacing: -0.5px;">
E Stores
</h1>
</td>
</tr>

<!-- Title -->
<tr>
<td align="center" style="padding: 0 40px 30px 40px;">
<h2 style="margin: 0; font-size: 24px; color: #1e1e1e; font-weight: bold;">
Verification OTP
</h2>
</td>
</tr>

<!-- Message -->
<tr>
<td align="center" style="padding: 0 40px 30px 40px;">
<p style="margin: 0; font-size: 16px; color: #4a4a4a; line-height: 24px;">
Please use the verification code below to complete your verification
</p>
</td>
</tr>

<!-- OTP Code -->
<tr>
<td align="center" style="padding: 0 40px 30px 40px;">
<div style="display: inline-block; padding: 20px 48px; background-color: #fafafa; border: 2px solid #f9a825; border-radius: 8px;">
<span th:text="${otp}" style="font-size: 36px; color: #f9a825; font-weight: bold; letter-spacing: 4px; font-family: 'Courier New', monospace;"></span>
</div>
</td>
</tr>

<!-- Note -->
<tr>
<td align="center" style="padding: 0 40px 40px 40px;">
<p style="margin: 0; font-size: 14px; color: #666666; font-style: italic; line-height: 20px;">
<span style="font-weight: bold; color: #f9a825;">Note:</span>
This verification code will expire in 5 minutes.
</p>
</td>
</tr>

<!-- Divider -->
<tr>
<td style="border-top: 1px solid #f0f0f0; margin: 0 40px;"></td>
</tr>

<!-- Footer -->
<tr>
<td style="padding: 30px 40px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<!-- Signature -->
<tr>
<td style="padding-bottom: 20px;">
<p style="margin: 0; color: #4a4a4a; font-size: 14px; line-height: 20px;">
Best regards,<br/>
<strong style="color: #1e1e1e;">E Stores Team</strong>
</p>
</td>
</tr>
<!-- Copyright & Links -->
<tr>
<td style="color: #666666; font-size: 12px; line-height: 1.5;">
<p style="margin: 0;">© 2024 E Stores. All rights reserved.</p>
<p style="margin: 8px 0 0 0;">
<span th:if="${unsubscribeLink}">
<a href="#" style="color: #f9a825; text-decoration: none;">Help & Support</a>
<span style="color: #666666; padding: 0 5px;">|</span>
</span>
<a href="#" style="color: #f9a825; text-decoration: none;">Privacy Policy</a>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>

<!-- Spacing for email clients -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
<tr>
<td style="height: 20px;"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
74 changes: 74 additions & 0 deletions E-Stores-API/src/main/resources/templates/simple-message.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>E Stores</title>
</head>
<body style="margin: 0; padding: 0; background-color: #f7f7f7; font-family: Arial, sans-serif;">
<!-- Main Table -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="background-color: #f7f7f7; padding: 20px;">
<tr>
<td align="center">
<!-- Content Table -->
<table border="0" cellpadding="0" cellspacing="0" width="600" style="background-color: #ffffff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<!-- Logo/Brand Section -->
<tr>
<td align="center" style="padding: 40px 40px 20px 40px;">
<h1 style="margin: 0; font-size: 28px; color: #f9a825; font-weight: bold; letter-spacing: -0.5px;">
E Stores
</h1>
</td>
</tr>

<!-- Title -->
<tr>
<td align="center" style="padding: 0 40px 30px 40px;">
<h2 style="margin: 0; font-size: 24px; color: #1e1e1e; font-weight: bold;">
Registration Successful
</h2>
</td>
</tr>

<!-- Message Content -->
<tr>
<td align="left" style="padding: 20px 40px;">
<div th:utext="${message}" style="color: #1e1e1e; font-size: 16px; line-height: 1.6;"></div>
</td>
</tr>

<!-- Footer -->
<tr>
<td style="padding: 30px 40px;">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<!-- Signature -->
<tr>
<td style="padding-bottom: 20px;">
<p style="margin: 0; color: #4a4a4a; font-size: 14px; line-height: 20px;">
Best regards,<br/>
<strong style="color: #1e1e1e;">E Stores Team</strong>
</p>
</td>
</tr>
<!-- Copyright & Links -->
<tr>
<td style="color: #666666; font-size: 12px; line-height: 1.5;">
<p style="margin: 0;">© 2024 E Stores. All rights reserved.</p>
<p style="margin: 8px 0 0 0;">
<span th:if="${unsubscribeLink}">
<a href="#" style="color: #f9a825; text-decoration: none;">Help & Support</a>
<span style="color: #666666; padding: 0 5px;">|</span>
</span>
<a href="#" style="color: #f9a825; text-decoration: none;">Privacy Policy</a>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

0 comments on commit 4c36ed7

Please sign in to comment.