Skip to content

Commit

Permalink
✨ 회원가입 인증 코드 이메일 템플릿 구현으로 인한 이메일 전송 코드 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
uuuuuuuk committed Apr 28, 2024
1 parent 920f8e3 commit 0fc9fed
Showing 1 changed file with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,31 @@ 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()
sendAuthEmail(email, randomKey)
}

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 """
<div style='margin:100px;'>
<h1> 안녕하세요 MISO 입니다! </h1>
<br>
<h2><p>아래 인증번호를 인증 페이지로 돌아가 입력해 주세요. 이용해 주셔서 감사합니다!<p></h2>
<br>
<div align='center' style='border:1px solid black; font-family:verdana';>
<h3 style='color:blue;'>인증번호는 다음과 같습니다!</h3>
<div style='font-size:130%'>
인증번호 : <strong>$randomKey</strong><div><br/>
</div>
""".trimIndent()
}

private fun saveEmailToRepository(email: String, randomKey: String) {
commandEmailPort.saveEmail(
Email(
Expand All @@ -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)
}

Expand All @@ -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)
}
}

0 comments on commit 0fc9fed

Please sign in to comment.