|
| 1 | +package devkor.com.teamcback.domain.common; |
| 2 | + |
| 3 | +import devkor.com.teamcback.domain.suggestion.entity.SuggestionImage; |
| 4 | +import jakarta.mail.MessagingException; |
| 5 | +import jakarta.mail.internet.MimeMessage; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.mail.SimpleMailMessage; |
| 8 | +import org.springframework.mail.javamail.JavaMailSender; |
| 9 | +import org.springframework.mail.javamail.MimeMessageHelper; |
| 10 | +import org.springframework.scheduling.annotation.Async; |
| 11 | +import org.springframework.stereotype.Service; |
| 12 | + |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +@Service |
| 18 | +public class EmailService { |
| 19 | + |
| 20 | + private static final String SUGGESTION_TITLE = "[고대로] 건의사항 추가됨 - 제목: "; |
| 21 | + private static final String SENDER = "고대로팀 <leeyejin113@gmail.com>"; |
| 22 | + |
| 23 | + @Value("${staff.emails}") |
| 24 | + private String notifyEmails; |
| 25 | + |
| 26 | + @Value("${metrics.environment}") |
| 27 | + private String env; |
| 28 | + |
| 29 | + private final JavaMailSender mailSender; |
| 30 | + |
| 31 | + public EmailService(JavaMailSender mailSender) { |
| 32 | + this.mailSender = mailSender; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * 건의사항 이메일 전송 |
| 37 | + */ |
| 38 | + @Async |
| 39 | + public void sendNotificationMessage(String title, String writer, String type, String content, List<SuggestionImage> images) throws MessagingException { |
| 40 | + |
| 41 | + // 내용 설정 |
| 42 | + Map<String, String> contents = new HashMap<>(); |
| 43 | + contents.put("건의제목", title); |
| 44 | + contents.put("건의종류", type); |
| 45 | + contents.put("건의내용", content); |
| 46 | + |
| 47 | + // 전송 |
| 48 | + sendHtmlEmail(SUGGESTION_TITLE + title, "건의", writer, contents, images); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * 일반 이메일 전송 |
| 53 | + */ |
| 54 | + private void sendSimpleEmail(String title, String type, String writer, Map<String, String> contents, List<SuggestionImage> images) { |
| 55 | + |
| 56 | + for(String notifyEmail : notifyEmails.split(",")) { |
| 57 | + SimpleMailMessage message = new SimpleMailMessage(); |
| 58 | + message.setFrom(SENDER); // 보내는 사람 |
| 59 | + message.setTo(notifyEmail); // 받는 사람 |
| 60 | + message.setSubject(title); // 제목 |
| 61 | + |
| 62 | + // 내용 |
| 63 | + String content = writer +"(으)로부터" + type + "가 추가되었습니다!"; |
| 64 | + for(String key : contents.keySet()) { |
| 65 | + content += "\n - " + key + ": " + contents.get(key); |
| 66 | + } |
| 67 | + |
| 68 | + // 첨부파일 |
| 69 | + if(images != null) { |
| 70 | + content += "\n - 첨부 파일:"; |
| 71 | + for (int i = 0; i < images.size(); i++) { |
| 72 | + content += "\n[" + (i+1) + "] link: " + images.get(i).getImageUrl(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + content += "\n* 서버 환경: " + env; |
| 77 | + |
| 78 | + message.setText(content); |
| 79 | + |
| 80 | + mailSender.send(message); |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * HTML 이메일 전송 |
| 87 | + */ |
| 88 | + private void sendHtmlEmail(String title, String type, String writer, Map<String, String> contents, List<SuggestionImage> images) throws MessagingException { |
| 89 | + |
| 90 | + for(String notifyEmail : notifyEmails.split(",")) { |
| 91 | + |
| 92 | + MimeMessage message = mailSender.createMimeMessage(); |
| 93 | + |
| 94 | + // true: multipart 메시지 (첨부파일 가능), "UTF-8": 문자 인코딩 |
| 95 | + MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); |
| 96 | + |
| 97 | + helper.setFrom(SENDER); |
| 98 | + helper.setTo(notifyEmail); |
| 99 | + helper.setSubject(title); |
| 100 | + |
| 101 | + // 내용 |
| 102 | + String html = "<h2 style='color:green;'>" + writer +"(으)로부터" + type + "가 추가되었습니다!</h2>"; |
| 103 | + for(String key : contents.keySet()) { |
| 104 | + html += "<p><strong>" + key + "</strong><br>" + contents.get(key) + "</p>"; |
| 105 | + } |
| 106 | + |
| 107 | + // 첨부파일 |
| 108 | + if(images != null) { |
| 109 | + html += "<p><strong>첨부 파일:</strong><br>"; |
| 110 | + for (int i = 0; i < images.size(); i++) { |
| 111 | + html += "<a href='" + images.get(i).getImageUrl() + "'>[" + (i+1) + "]</a> "; |
| 112 | + } |
| 113 | + html += "</p>"; |
| 114 | + } |
| 115 | + |
| 116 | + html += "<p>* 서버 환경: " + env + "</p>"; |
| 117 | + |
| 118 | + helper.setText(html, true); // HTML 모드 |
| 119 | + |
| 120 | + mailSender.send(message); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments