diff --git a/build.gradle.kts b/build.gradle.kts index 68cedeb3..ac7c6c8a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -75,6 +75,9 @@ dependencies { // web_flux implementation(Dependencies.WEB_FLUX) + + // thymeleaf + implementation(Dependencies.THYMELEAF) } tasks.withType { diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt index a9f2726b..ad1e4554 100644 --- a/buildSrc/src/main/kotlin/Dependencies.kt +++ b/buildSrc/src/main/kotlin/Dependencies.kt @@ -42,4 +42,7 @@ object Dependencies { // web_flux const val WEB_FLUX = "org.springframework.boot:spring-boot-starter-webflux" + + // thymeleaf + const val THYMELEAF = "org.springframework.boot:spring-boot-starter-thymeleaf" } \ No newline at end of file diff --git a/src/main/kotlin/andreas311/miso/thirdparty/email/EmailSendAdapter.kt b/src/main/kotlin/andreas311/miso/thirdparty/email/EmailSendAdapter.kt index ec02b66b..98e2ff04 100644 --- a/src/main/kotlin/andreas311/miso/thirdparty/email/EmailSendAdapter.kt +++ b/src/main/kotlin/andreas311/miso/thirdparty/email/EmailSendAdapter.kt @@ -7,13 +7,16 @@ import andreas311.miso.domain.email.domain.Email import org.springframework.mail.javamail.JavaMailSender import org.springframework.mail.javamail.MimeMessageHelper import org.springframework.stereotype.Component +import org.thymeleaf.context.Context +import org.thymeleaf.spring5.SpringTemplateEngine import java.util.* import javax.mail.MessagingException @Component class EmailSendAdapter( private val javaMailSender: JavaMailSender, - private val commandEmailPort: CommandEmailPort + private val commandEmailPort: CommandEmailPort, + private val springTemplateEngine: SpringTemplateEngine ) : EmailSendPort { override fun sendEmailAuthKey(email: String) { val randomKey = createRandomKey() @@ -21,32 +24,14 @@ class EmailSendAdapter( } private fun sendAuthEmail(email: String, randomKey: String) { - val subject = "MISO 인증번호가 도착했습니다!" - val content = buildEmailContent(randomKey) - try { - sendEmail(email, subject, content) + sendEmail(email, randomKey) } catch (e: MessagingException) { throw EmailSendFailedException() } saveEmailToRepository(email, randomKey) } - private fun buildEmailContent(randomKey: String): String { - return """ -
-

안녕하세요 MISO 입니다!

-
-

아래 인증번호를 인증 페이지로 돌아가 입력해 주세요. 이용해 주셔서 감사합니다!

-
-
-

인증번호는 다음과 같습니다!

-
- 인증번호 : $randomKey

-
- """.trimIndent() - } - private fun saveEmailToRepository(email: String, randomKey: String) { commandEmailPort.saveEmail( Email( @@ -58,12 +43,13 @@ class EmailSendAdapter( ) } - private fun sendEmail(email: String, subject: String, content: String) { + private fun sendEmail(email: String, randomKey: String) { val mimeMessage = javaMailSender.createMimeMessage() val helper = MimeMessageHelper(mimeMessage, true, "utf-8") + val mailTemplate = createMailTemplate(randomKey) helper.setTo(email) - helper.setSubject(subject) - helper.setText(content, true) + helper.setSubject("MISO 인증번호가 도착했습니다!") + helper.setText(mailTemplate, true) javaMailSender.send(mimeMessage) } @@ -74,4 +60,15 @@ class EmailSendAdapter( return randomKey.toString() } + + private fun createMailTemplate(randomKey: String): String { + val content = Context() + val randomKey = randomKey + content.setVariables( + mapOf( + "randomKey" to randomKey + ) + ) + return springTemplateEngine.process("mailTemplate", content) + } } \ No newline at end of file diff --git a/src/main/resources/templates/mailTemplate.html b/src/main/resources/templates/mailTemplate.html new file mode 100644 index 00000000..65a54095 --- /dev/null +++ b/src/main/resources/templates/mailTemplate.html @@ -0,0 +1,68 @@ + + + + + + Document + + +
+
+
+ miso_icon +

+ 미소 이메일 인증 번호 +

+
+
+ 안녕하세요 MISO에요!
+ 미소를 이용해 주셔서 감사해요 :)
+ 위 4자리 숫자를 입력하시면 회원가입 절차가 완료돼요! +
+
+
+
+
환경을 웃음으로 바꾸다, MISO
+ +
+
+ + \ No newline at end of file