Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 이메일 전송 api 구현 #12

Merged
merged 9 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
- name: 🍃 application.yml 파일을 생성합니다.
run: |
cd ./src/main
mkdir resources
cd ./resources
touch ./application.yml
echo "$APPLICATION" > ./application.yml
Expand Down
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ dependencies {
// mysql
runtimeOnly 'com.mysql:mysql-connector-j'

// javamail
implementation 'org.springframework.boot:spring-boot-starter-mail'

// thymeleaf
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

// open feign
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:4.0.3'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ public enum ErrorCode {
* 400 Bad Request
*/
BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),

/**
* 401 Unauthorized
*/
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "리소스 접근 권한이 없습니다."),
INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "액세스 토큰의 형식이 올바르지 않습니다. Bearer 타입을 확인해 주세요."),
INVALID_ACCESS_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "액세스 토큰의 값이 올바르지 않습니다."),
EXPIRED_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "액세스 토큰이 만료되었습니다. 재발급 받아주세요."),
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "리프레시 토큰의 형식이 올바르지 않습니다."),
INVALID_REFRESH_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "리프레시 토큰의 값이 올바르지 않습니다."),
EXPIRED_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 만료되었습니다. 다시 로그인해 주세요."),
NOT_MATCH_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "일치하지 않는 리프레시 토큰입니다."),
INVALID_PLATFORM_TYPE(HttpStatus.UNAUTHORIZED, "일치하지 않은 로그인 형식입니다."),
INVALID_NAVER_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "일치하지 않은 네이버 토큰입니다."),

/**
* 401 Unauthorized
*/
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "리소스 접근 권한이 없습니다."),
INVALID_PLATFORM_TYPE(HttpStatus.UNAUTHORIZED, "올바르지 않은 로그인 방식입니다."),
INVALID_NAVER_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "네이버 토큰이 올바르지 않습니다."),
UNAUTHORIZED_GOOGLE_SMTP(HttpStatus.UNAUTHORIZED, "메일 전송을 실패했습니다."),

/**
* 403 Forbidden
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.kusithm.hdmedi_server.infra.smtp;

import com.kusithm.hdmedi_server.global.error.exception.UnauthorizedException;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;

import static com.kusithm.hdmedi_server.global.error.exception.ErrorCode.UNAUTHORIZED_GOOGLE_SMTP;

@Slf4j
@RequiredArgsConstructor
@Component
public class GoogleEmailProvider {
private static final String SERVER_MAIL = "[email protected]";
private static final String EMAIL_TITLE = "HDmedi";
private final JavaMailSender javaMailSender;
private final SpringTemplateEngine springTemplateEngine;

public void sendEmail(String toEmail, String content) {
MimeMessage emailForm = createEmailForm(toEmail, content);
javaMailSender.send(emailForm);
}

private MimeMessage createEmailForm(String email, String content) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
message.addRecipients(MimeMessage.RecipientType.TO, email);
message.setSubject(EMAIL_TITLE);
message.setFrom(SERVER_MAIL);
message.setText(setContext(content), "utf-8", "html");
} catch (MessagingException e) {
log.error("Failed to send Email", e);
throw new UnauthorizedException(UNAUTHORIZED_GOOGLE_SMTP);
}
return message;
}

private String setContext(String code) {
Context context = new Context();
context.setVariable("code", code);
return springTemplateEngine.process("mail", context);
}
}
21 changes: 21 additions & 0 deletions src/main/resources/templates/mail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<body>
<div style="margin:100px;">
<h1> 안녕하세요.</h1>
<h1> 알고리즘 관리 서비스 CODEBOX 입니다.</h1>
<br>
<p> 아래 코드를 회원가입 창으로 돌아가 입력해주세요.</p>
<br>

<div align="center" style="border:1px solid black; font-family:verdana;">
<h3 style="color:blue"> 회원가입 인증 코드 입니다. </h3>
<div style="font-size:130%" th:text="${code}"> </div>
</div>
<br/>
</div>


</body>
</html>