-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from 9oormthon-univ/52-feature-send-notificati…
…on-email-upon-donation-completion #52 - By Using Springboot mail Library Implement Sending Notification…
- Loading branch information
Showing
12 changed files
with
290 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/java/com/example/mymoo/domain/email/EmailClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.example.mymoo.domain.email; | ||
|
||
import com.example.mymoo.domain.donationusage.entity.DonationUsage; | ||
import com.example.mymoo.domain.email.dto.EmailSendDTO; | ||
import com.example.mymoo.domain.email.exception.EmailException; | ||
import com.example.mymoo.domain.email.exception.EmailExceptionDetails; | ||
import jakarta.annotation.PostConstruct; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.stereotype.Service; | ||
import org.thymeleaf.context.Context; | ||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class EmailClient { | ||
|
||
private final JavaMailSender javaMailSender; | ||
private final SpringTemplateEngine templateEngine; | ||
|
||
public void sendDonationUsageMail(EmailSendDTO send){ | ||
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); | ||
try{ | ||
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, "UTF-8"); | ||
mimeMessageHelper.setTo(send.getEmail()); | ||
mimeMessageHelper.setSubject("[마이무] 감사합니다! 당신의 후원이 사용되었습니다!"); | ||
mimeMessageHelper.setText(setContext("email", send), true); | ||
javaMailSender.send(mimeMessage); | ||
log.info("이메일 전송 성공"); | ||
} catch (MessagingException e){ | ||
log.info("이메일 전송 실패"); | ||
throw new EmailException(EmailExceptionDetails.EMAIL_SEND_FAILED); | ||
} | ||
} | ||
|
||
public String setContext(String type, EmailSendDTO send){ | ||
Context context = new Context(); | ||
context.setVariable("storeName", send.getStoreName()); | ||
context.setVariable("usedPrice", send.getUsedPrice()); | ||
context.setVariable("childName", send.getChildName()); | ||
context.setVariable("usedTime", send.getUsedTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | ||
context.setVariable("donatedTime", send.getDonatedTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); | ||
return templateEngine.process(type, context); | ||
} | ||
|
||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/example/mymoo/domain/email/dto/EmailSendDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.example.mymoo.domain.email.dto; | ||
|
||
import com.example.mymoo.domain.donationusage.entity.DonationUsage; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data @Builder | ||
public class EmailSendDTO { | ||
private String email; | ||
private String storeName; | ||
private Long usedPrice; | ||
private String childName; | ||
private LocalDateTime usedTime; | ||
private LocalDateTime donatedTime; | ||
|
||
public static EmailSendDTO from(DonationUsage donationUsage){ | ||
|
||
String email = donationUsage.getDonation().getAccount().getEmail(); | ||
|
||
if (email.contains("_")){ | ||
email = email.substring(email.lastIndexOf("_")+1); | ||
} | ||
|
||
return EmailSendDTO.builder() | ||
.email(email) | ||
.storeName(donationUsage.getDonation().getStore().getName()) | ||
.usedPrice(donationUsage.getDonation().getPoint()) | ||
.childName(donationUsage.getChild().getAccount().getNickname()) | ||
.usedTime(donationUsage.getModifiedAt()) | ||
.donatedTime(donationUsage.getCreatedAt()) | ||
.build(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/mymoo/domain/email/exception/EmailException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.mymoo.domain.email.exception; | ||
|
||
import com.example.mymoo.global.exception.CustomException; | ||
|
||
public class EmailException extends CustomException { | ||
public EmailException(EmailExceptionDetails paymentExceptionDetails){ | ||
super(paymentExceptionDetails); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/mymoo/domain/email/exception/EmailExceptionDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.example.mymoo.domain.email.exception; | ||
|
||
import com.example.mymoo.global.exception.ExceptionDetails; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum EmailExceptionDetails implements ExceptionDetails { | ||
// 가게 id가 store 테이블에 존재하지 않을 때 | ||
EMAIL_SEND_FAILED(HttpStatus.BAD_REQUEST, "이메일 전송이 실패했습니다."), | ||
; | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/java/com/example/mymoo/global/auth/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/example/mymoo/global/config/EmailConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.example.mymoo.global.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
|
||
import java.util.Properties; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class EmailConfig { | ||
private static final String MAIL_SMTP_AUTH = "mail.smtp.auth"; | ||
private static final String MAIL_DEBUG = "mail.smtp.debug"; | ||
private static final String MAIL_CONNECTION_TIMEOUT = "mail.smtp.connectiontimeout"; | ||
private static final String MAIL_SMTP_STARTTLS_ENABLE = "mail.smtp.starttls.enable"; | ||
|
||
// SMTP 서버 | ||
@Value("${spring.mail.host}") | ||
private String host; | ||
|
||
// 계정 | ||
@Value("${spring.mail.username}") | ||
private String username; | ||
|
||
// 비밀번호 | ||
@Value("${spring.mail.password}") | ||
private String password; | ||
|
||
// 포트번호 | ||
@Value("${spring.mail.port}") | ||
private int port; | ||
|
||
@Value("${spring.mail.properties.mail.smtp.auth}") | ||
private boolean auth; | ||
|
||
// @Value("${spring.mail.properties.mail.smtp.debug}") | ||
// private boolean debug; | ||
|
||
@Value("${spring.mail.properties.mail.smtp.timeout}") | ||
private int connectionTimeout; | ||
|
||
@Value("${spring.mail.properties.mail.starttls.enable}") | ||
private boolean startTlsEnable; | ||
|
||
@Bean | ||
public JavaMailSender javaMailService() { | ||
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); | ||
javaMailSender.setHost(host); | ||
javaMailSender.setUsername(username); | ||
javaMailSender.setPassword(password); | ||
javaMailSender.setPort(port); | ||
|
||
Properties properties = javaMailSender.getJavaMailProperties(); | ||
properties.put(MAIL_SMTP_AUTH, auth); | ||
// properties.put(MAIL_DEBUG, debug); | ||
properties.put(MAIL_CONNECTION_TIMEOUT, connectionTimeout); | ||
properties.put(MAIL_SMTP_STARTTLS_ENABLE, startTlsEnable); | ||
|
||
javaMailSender.setJavaMailProperties(properties); | ||
javaMailSender.setDefaultEncoding("UTF-8"); | ||
|
||
return javaMailSender; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org" lang="ko"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>후원 사용 내역 안내</title> | ||
<link th:href="@{css/email.css}" rel="stylesheet"> | ||
</head> | ||
<body style="font-family: 'Pretendard', sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
background-color: #f9f9f9; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh;"> | ||
<div class="receipt-container" style="background-color: #fff; | ||
border: 1px solid #ddd; | ||
border-radius: 8px; | ||
width: 360px; | ||
padding: 20px; | ||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);"> | ||
<div class="receipt-header" style="text-align: center; | ||
margin-bottom: 20px;"> | ||
<img width="107" alt="KakaoTalk_20241206_203440105" src="https://github.com/user-attachments/assets/576bd699-c42e-4138-b128-88a44f1f2f05"> | ||
<h1 class="receipt-title" style="font-size: 18px; | ||
font-weight: bold; | ||
color: #333; | ||
margin: 0;">마이무 결제 내역 안내</h1> | ||
</div> | ||
<div class="receipt-body" style="margin-top: 20px;"> | ||
<div class="receipt-item" style="display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 10px 0; | ||
border-bottom: 1px solid #ddd; | ||
font-size: 14px;"> | ||
<span class="item-label" style="color: #555;">사용된 가게</span> | ||
<span class="item-value" th:text="${storeName}" style="color: #000; | ||
font-weight: bold;">15,000원</span> | ||
</div> | ||
<div class="receipt-item" style="display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 10px 0; | ||
border-bottom: 1px solid #ddd; | ||
font-size: 14px;"> | ||
<span class="item-label" style="color: #555;">금액</span> | ||
<span class="item-value" th:text="${usedPrice}" style="color: #000; | ||
font-weight: bold;">15,000원</span> | ||
</div> | ||
<div class="receipt-item" style="display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 10px 0; | ||
border-bottom: 1px solid #ddd; | ||
font-size: 14px;"> | ||
<span class="item-label" style="color: #555;">사용한 아동</span> | ||
<span class="item-value" th:text="${childName}" style="color: #000; | ||
font-weight: bold;">희망이</span> | ||
</div> | ||
<div class="receipt-item" style="display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 10px 0; | ||
border-bottom: 1px solid #ddd; | ||
font-size: 14px;"> | ||
<span class="item-label" style="color: #555;">사용한 시간</span> | ||
<span class="item-value" th:text="${usedTime}" style="color: #000; | ||
font-weight: bold;">2024-11-12 13:30</span> | ||
</div> | ||
<div class="receipt-item" style="display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
padding: 10px 0; | ||
border-bottom: 1px solid #ddd; | ||
font-size: 14px;"> | ||
<span class="item-label" style="color: #555;">후원한 시간</span> | ||
<span class="item-value" th:text="${donatedTime}" style="color: #000; | ||
font-weight: bold;">2024-12-05 14:30</span> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |