Skip to content

Commit

Permalink
[UPDATE] update mail template
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoi-kang committed Nov 15, 2023
1 parent 5fd1eeb commit f5a46a2
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 43 deletions.
40 changes: 0 additions & 40 deletions src/main/kotlin/healthiee/rest/lib/mail/MailSender.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package healthiee.rest.lib.mail.model

import java.util.*

data class MailBuilderParams(
val registered: Boolean,
val code: UUID,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package healthiee.rest.lib.mail.model

data class MailSenderParams(
val targetEmail: String,
val subject: String,
val body: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package healthiee.rest.lib.mail.sender

import com.amazonaws.services.simpleemail.AmazonSimpleEmailService
import com.amazonaws.services.simpleemail.model.Body
import com.amazonaws.services.simpleemail.model.Content
import com.amazonaws.services.simpleemail.model.Destination
import com.amazonaws.services.simpleemail.model.Message
import com.amazonaws.services.simpleemail.model.SendEmailRequest
import healthiee.rest.lib.mail.model.MailSenderParams
import org.springframework.stereotype.Component

@Component
class AmazonSESMailSender(
private val ses: AmazonSimpleEmailService,
) : MailSender {

override fun send(params: MailSenderParams) {
val mailRequest = SendEmailRequest()
.withSource("[email protected]")
.withDestination(Destination().withToAddresses(params.targetEmail))
.withMessage(
Message()
.withSubject(Content(params.subject).withCharset(Charsets.UTF_8.name()))
.withBody(Body().withHtml(Content(params.body)))
)

ses.sendEmail(mailRequest)
}

}
10 changes: 10 additions & 0 deletions src/main/kotlin/healthiee/rest/lib/mail/sender/MailSender.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package healthiee.rest.lib.mail.sender

import healthiee.rest.lib.mail.model.MailSenderParams


interface MailSender {

fun send(params: MailSenderParams)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package healthiee.rest.lib.mail.template

import healthiee.rest.lib.mail.model.MailBuilderParams
import org.springframework.stereotype.Component
import org.thymeleaf.context.Context
import org.thymeleaf.spring6.SpringTemplateEngine

@Component
class AuthMailBodyBuilder(
private val templateEngine: SpringTemplateEngine,
) : MailBuilder {

override fun makeSubject(params: MailBuilderParams): String {
return "[Healthiee] ${if (params.registered) "로그인" else "회원가입"}"
}

override fun makeBody(params: MailBuilderParams): String {
val endpoint =
if (params.registered) "email-login"
else "authcompleted"
val context = Context().apply {
setVariable("url", "http://localhost:3000/${endpoint}?code=${params.code}")
setVariable("text", if (params.registered) "로그인" else "회원가입")
}
return templateEngine.process("code", context)
}

}
10 changes: 10 additions & 0 deletions src/main/kotlin/healthiee/rest/lib/mail/template/MailBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package healthiee.rest.lib.mail.template

import healthiee.rest.lib.mail.model.MailBuilderParams

interface MailBuilder {

fun makeSubject(params: MailBuilderParams): String
fun makeBody(params: MailBuilderParams): String

}
17 changes: 15 additions & 2 deletions src/main/kotlin/healthiee/rest/service/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import healthiee.rest.lib.error.ApiException
import healthiee.rest.lib.error.ApplicationErrorCode.FORBIDDEN_INVALID_REFRESH_TOKEN
import healthiee.rest.lib.error.ApplicationErrorCode.NOT_FOUND_CODE
import healthiee.rest.lib.error.ApplicationErrorCode.NOT_FOUND_MEMBER
import healthiee.rest.lib.mail.MailSender
import healthiee.rest.lib.mail.model.MailBuilderParams
import healthiee.rest.lib.mail.model.MailSenderParams
import healthiee.rest.lib.mail.sender.MailSender
import healthiee.rest.lib.mail.template.MailBuilder
import healthiee.rest.lib.uploader.MediaDomainType
import healthiee.rest.lib.uploader.S3Uploader
import healthiee.rest.repository.auth.EmailAuthRepository
Expand All @@ -42,6 +45,7 @@ class AuthService(
private val memberRepository: MemberRepository,
private val tokenRepository: TokenRepository,
private val hashtagRepository: HashtagRepository,
private val mailBuilder: MailBuilder,
private val mailSender: MailSender,
private val jwtTokenProvider: JwtTokenProvider,
private val passwordEncoder: PasswordEncoder,
Expand All @@ -56,7 +60,16 @@ class AuthService(

val emailAuth = EmailAuth.createEmailAuth(request.email)
emailAuthRepository.save(emailAuth)
mailSender.send(request.email, registered, emailAuth.code)

val mailBuilderParams = MailBuilderParams(registered, emailAuth.code)

mailSender.send(
MailSenderParams(
request.email,
mailBuilder.makeSubject(mailBuilderParams),
mailBuilder.makeBody(mailBuilderParams),
)
)

return AuthResponse(registered)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/mail-templates/code.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
style="width: 200px; height: 40px; margin-bottom: 12px; max-width: 100%; margin: 0 auto;">
</div>
<div style="font-size: 12px; max-width: 100%; width: 400px; margin: 0 auto; padding: 1rem; text-align: justify; background: #f8f9fa; border: 1px solid #dee2e6; box-sizing: border-box; border-radius: 20px; color: #000000; margin-top: 0.5rem; box-sizing: border-box;">
로그인을 계속하시려면 하단의 링크를 클릭하세요. 만약에 실수로 요청하셨거나, 본인이 요청하지 않았다면 이 메일을 무시하세요.
<span th:text="${text}"></span> 계속하시려면 하단의 링크를 클릭하세요. 만약에 실수로 요청하셨거나, 본인이 요청하지 않았다면 이 메일을 무시하세요.
</div>
<div style="text-decoration: none; text-align: center;">
<a th:href="@{${url}}">
Expand Down

0 comments on commit f5a46a2

Please sign in to comment.