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

[feature/56-sendMail] 쥬스레터 발송 API #57

Merged
merged 1 commit into from
Sep 27, 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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ dependencies {

implementation 'com.googlecode.json-simple:json-simple:1.1.1'

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


}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public enum BaseResponseStatus {
/**
* 4000: DB, Server 오류
*/
DATABASE_ERROR(false, 4000, "데이터베이스 연결에 실패했습니다.");
DATABASE_ERROR(false, 4000, "데이터베이스 연결에 실패했습니다."),
MAIL_DELIVERY_ERROR(false, 4001, "쥬스레터 메일 발송에 실패했습니다.");

private final boolean isSuccess;
private final int code;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/ewhatever/qna/mail/MailController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.ewhatever.qna.mail;

import com.ewhatever.qna.comment.dto.PostCommentReq;
import com.ewhatever.qna.comment.service.CommentService;
import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.Base.BaseResponse;
import com.ewhatever.qna.mail.dto.MailDto;
import com.ewhatever.qna.mail.service.MailService;
import jakarta.mail.MessagingException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.SUCCESS;

@RequestMapping("/letter")
@RestController
@RequiredArgsConstructor
public class MailController {

private final MailService mailService;

@ResponseBody
@PostMapping
public BaseResponse<String> addComment(@RequestBody MailDto mailDto) throws BaseException {
mailService.sendMail(mailDto);
return new BaseResponse<>(SUCCESS);
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/ewhatever/qna/mail/dto/MailDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ewhatever.qna.mail.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class MailDto {
private String title;
private String contentTitle;
private String content;
}
68 changes: 68 additions & 0 deletions src/main/java/com/ewhatever/qna/mail/service/MailService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.ewhatever.qna.mail.service;

import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.mail.dto.MailDto;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;

import java.util.HashMap;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.MAIL_DELIVERY_ERROR;

@Service
@AllArgsConstructor
@Slf4j
public class MailService {

private JavaMailSender emailSender;
private final SpringTemplateEngine templateEngine;

@Async
public void sendMail(MailDto mailDto) throws BaseException {

log.info("*** 메일 전송 시작 ***");

try{
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

//메일 제목 설정
helper.setSubject(mailDto.getTitle());

//TODO : DB에서 읽어와서 나중에 구독한 사람들로 설정하기
helper.setTo(new String[]{"hahaha329@ewhain.net"});

//템플릿에 전달할 데이터 설정
HashMap<String, String> emailValues = new HashMap<>();
emailValues.put("title", mailDto.getContentTitle());
emailValues.put("content", mailDto.getContent());


Context context = new Context();
emailValues.forEach((key, value)->{
context.setVariable(key, value);
});

//메일 내용 설정 : 템플릿 프로세스
String html = templateEngine.process("juiceLetter", context);
helper.setText(html, true);

//메일 보내기
emailSender.send(message);
} catch(MessagingException e) {
throw new BaseException(MAIL_DELIVERY_ERROR);
}

log.info("*** 메일 전송 완료 ***");


}
}
146 changes: 146 additions & 0 deletions src/main/resources/templates/juiceLetter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Title</title>
</head>
<body>
<div style="max-width: 595px; margin: 0 auto;">
<table
cellpadding="0"
cellspacing="0"
style="
width: 100%;
margin: 0 auto;
background-color: #fff;
-webkit-text-size-adjust: 100%;
text-align: left;
"
>
<tbody>
<tr>
<td colspan="3" height="30"></td>
</tr>
<tr>
<td width="21"></td>
<td>
<table
cellpadding="0"
cellspacing="0"
style="width: 100%; margin: 0; padding: 0;"
>
<tbody>
<tr>
<td style="margin: 0; padding: 0;">
<a
href="https://ewhatever-front.vercel.app/"
rel="noreferrer noopener"
target="_blank"
><img
src="https://github.com/Haeun-Y/gyeo-bang-al/assets/70390323/43e23443-7ca3-48a7-ab1f-9d0fc2d64e85"
width="160"
height="52"
alt="Juitcy"
style="border: 0; margin-right: 5px;"
loading="lazy"
/>
</a>
</td>
</tr>
<tr>
<td height="11"></td>
</tr>
</tbody>
</table>
</td>
<td width="21"></td>
</tr>

<tr>
<td width="21"></td>
<td>
<table
cellpadding="0"
cellspacing="0"
style="width: 100%; margin: 0; padding: 0;"
>
<tbody>
<tr>
<td
style="
font-size: 28px;
font-family: '나눔고딕', NanumGothic, '맑은고딕',
Malgun Gothic, '돋움', Dotum, Helvetica,
'Apple SD Gothic Neo', Sans-serif;
color: #424240;

line-height: 34px;
vertical-align: top;
"
>
<span style="color: #ff8267;">
<strong>쥬스레터 : </strong></span
>
<strong>"</strong>
</td>
<td
style="
font-size: 28px;
font-family: '나눔고딕', NanumGothic, '맑은고딕',
Malgun Gothic, '돋움', Dotum, Helvetica,
'Apple SD Gothic Neo', Sans-serif;
color: #424240;

line-height: 34px;
vertical-align: top;
"
th:text="${title}"
>
<strong>"</strong>
</td>
</tr>
<tr>
<td height="13"></td>
</tr>
<tr>
<td height="22"></td>
</tr>
</tbody>
</table>
</td>
<td width="21"></td>
</tr>
<tr>
<td colspan="3" height="1" style="background: #e5e5e5;"></td>
</tr>
<tr>
<td colspan="3" height="26"></td>
</tr>
<tr>
<td width="21"></td>
<td th:text="${content}"></td>
<td width="21"></td>
</tr>
<tr>
<td width="21"></td>
<td>
<img
src="https://github.com/Haeun-Y/gyeo-bang-al/assets/70390323/3307f733-6096-4117-b522-e7a486e972d0"
width="300"
height="415"
alt="Juitcy"
style="border: 0; margin-right: 5px;"
loading="lazy"
/>
</td>
<td width="21"></td>
</tr>
</tbody>
</table>
</div>
<!--[if mso]>
</td></tr>
</table>
<![endif]-->
</body>
</html>