From 6563a5818ff0c13e65f3f898e0ea6850cf7d5ac9 Mon Sep 17 00:00:00 2001 From: baegteun Date: Fri, 10 Mar 2023 12:20:28 +0900 Subject: [PATCH 1/3] =?UTF-8?q?:sparkles:=20::=20=EC=9D=B4=EB=A9=94?= =?UTF-8?q?=EC=9D=BC=20=ED=85=9C=ED=94=8C=EB=A6=BF=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 1 + .../domain/email/services/MailSendService.kt | 22 +++- .../com/msg/gauth/global/mail/MailConfig.kt | 5 + src/main/resources/templates/mail.html | 107 ++++++++++++++++++ 4 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 src/main/resources/templates/mail.html diff --git a/build.gradle.kts b/build.gradle.kts index fd769f3e..5f6e999c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,6 +30,7 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-security") implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-mail") + implementation("org.springframework.boot:spring-boot-starter-thymeleaf") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.springframework.boot:spring-boot-starter-validation") implementation("io.jsonwebtoken:jjwt-api:0.11.5") diff --git a/src/main/kotlin/com/msg/gauth/domain/email/services/MailSendService.kt b/src/main/kotlin/com/msg/gauth/domain/email/services/MailSendService.kt index 2fecf8be..dcd1de39 100644 --- a/src/main/kotlin/com/msg/gauth/domain/email/services/MailSendService.kt +++ b/src/main/kotlin/com/msg/gauth/domain/email/services/MailSendService.kt @@ -12,6 +12,9 @@ import org.springframework.scheduling.annotation.Async import org.springframework.scheduling.annotation.EnableAsync import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional +import org.thymeleaf.TemplateEngine +import org.thymeleaf.context.Context +import org.thymeleaf.spring5.SpringTemplateEngine import java.util.* import javax.mail.Message import javax.mail.MessagingException @@ -19,7 +22,8 @@ import javax.mail.MessagingException @Service class MailSendService( private val mailSender: JavaMailSender, - private val emailAuthRepository: EmailAuthRepository + private val emailAuthRepository: EmailAuthRepository, + private val templateEngine: TemplateEngine ) { fun execute(emailSendDto: EmailSendDto) { @@ -36,19 +40,27 @@ class MailSendService( ) if(authEntity.authentication) throw AlreadyAuthenticatedEmailException() - if (authEntity.attemptCount >= 3) throw ManyRequestEmailAuthException() + if (authEntity.attemptCount >= 5) throw ManyRequestEmailAuthException() val updateEmailAuth = authEntity.resendEmailAuth(value) emailAuthRepository.save(updateEmailAuth) try { val message = mailSender.createMimeMessage() - val msg = - "인증하기" + val mailTemplate = createMailTemplate(email, value) message.addRecipients(Message.RecipientType.TO, emailSendDto.email) message.subject = "[GAuth] 이메일 인증" - message.setText(msg, "utf-8", "html") + message.setText(mailTemplate, "utf-8", "html") mailSender.send(message) } catch (ex: MessagingException) { throw MessageSendFailException() } } + + private fun createMailTemplate(email: String, code: String): String { + val context = Context() + context.setVariables(mapOf( + "email" to email, + "code" to code + )) + return templateEngine.process("mail", context) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/msg/gauth/global/mail/MailConfig.kt b/src/main/kotlin/com/msg/gauth/global/mail/MailConfig.kt index 40cece23..a7bc50b5 100644 --- a/src/main/kotlin/com/msg/gauth/global/mail/MailConfig.kt +++ b/src/main/kotlin/com/msg/gauth/global/mail/MailConfig.kt @@ -5,6 +5,8 @@ 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 org.thymeleaf.TemplateEngine +import org.thymeleaf.spring5.SpringTemplateEngine @Configuration class MailConfig( @@ -27,4 +29,7 @@ class MailConfig( this.javaMailProperties["mail.smtp.starttls.required"] = true } + @Bean + fun templateEngine(): TemplateEngine = + SpringTemplateEngine() } \ No newline at end of file diff --git a/src/main/resources/templates/mail.html b/src/main/resources/templates/mail.html new file mode 100644 index 00000000..2dd03636 --- /dev/null +++ b/src/main/resources/templates/mail.html @@ -0,0 +1,107 @@ + + + + + + + Document + + + +
+
+
+ + + + + + + + + +

+ 메일인증 안내입니다. +

+
+

+ GAuth에서 보낸 인증 메일입니다.
+ 이메일 인증을 위해 아래 버튼을 눌러주세요. +

+ + 인증 확인 + +
+
+
+ + From e54345ff3dfd95b2d8b26e2adbf49dfda32248e3 Mon Sep 17 00:00:00 2001 From: baegteun Date: Fri, 10 Mar 2023 12:22:41 +0900 Subject: [PATCH 2/3] =?UTF-8?q?:pencil2:=20::=20=EC=8B=A4=EC=88=98?= =?UTF-8?q?=E3=85=97=E3=84=B9=20=EB=B0=94=EC=9D=B8=EB=94=A9=20=EC=95=88?= =?UTF-8?q?=ED=95=A8=20=E3=85=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/templates/mail.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/templates/mail.html b/src/main/resources/templates/mail.html index 2dd03636..854267d5 100644 --- a/src/main/resources/templates/mail.html +++ b/src/main/resources/templates/mail.html @@ -84,7 +84,7 @@

이메일 인증을 위해 아래 버튼을 눌러주세요.

{ mailVerificationService.execute(email, uuid) - return ResponseEntity.ok("완료되었습니다!
회원가입을 진행해주세요.") + return ResponseEntity.ok("완료되었습니다!
다음 단계를 진행해주세요.") } @GetMapping