From 0fc9fed1b4667fe29e48f4d23be1c24490d75d31 Mon Sep 17 00:00:00 2001 From: uuuuuuuk Date: Sun, 28 Apr 2024 22:43:01 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EC=9D=B8=EC=A6=9D=20=EC=BD=94=EB=93=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=A9=94=EC=9D=BC=20=ED=85=9C=ED=94=8C=EB=A6=BF=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=A9=94=EC=9D=BC=20=EC=A0=84=EC=86=A1=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miso/thirdparty/email/EmailSendAdapter.kt | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) 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