Skip to content

Commit

Permalink
Feat: 메일 전송 구현 (moyeothon#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
kduoh99 committed Nov 2, 2024
1 parent e6239f1 commit 1c64cab
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 45 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.hackathon.momento.team.application;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class EmailService {

private static final String FROM_EMAIL = "[email protected]";
private static final String TEAM_NAME = "TeamUp";

private final JavaMailSender mailSender;

public void sendMessage(String to, String body) {
try {
MimeMessage message = mailSender.createMimeMessage();

String subject = "[TeamUp] 요청하신 팀 빌딩이 완료되었습니다, 팀 정보를 확인해주세요.";
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);

mimeMessageHelper.setTo(to);
mimeMessageHelper.setFrom(FROM_EMAIL, TEAM_NAME);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(body, true);

mailSender.send(message);
log.info("Email sent to: {}", to);

} catch (MessagingException | UnsupportedEncodingException e) {
log.error("Email send failed to {}", to, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class TeamService {
private final TeamInfoRepository teamInfoRepository;
private final MemberRepository memberRepository;
private final RestTemplate restTemplate;
private final EmailService emailService;

@Value("${openai.api.url}")
private String apiURL;
Expand Down Expand Up @@ -77,6 +78,43 @@ public void executeTeamBuilding() {

List<TeamInfo> teamInfos = parseResToTeamInfo(resContent, requests);
teamInfoRepository.saveAll(teamInfos);

sendTeamCompletionEmails(teamInfos);
}
}

private void sendTeamCompletionEmails(List<TeamInfo> teamInfos) {
for (TeamInfo teamInfo : teamInfos) {
String teamName = teamInfo.getTeamName();
String description = teamInfo.getDescription();
List<TeamBuilding> teamMembers = teamInfo.getTeamBuildings();

StringBuilder emailBody = new StringBuilder();
emailBody.append("<div style='font-family: Arial, sans-serif; max-width: 600px; margin: auto;'>")
.append("<h1 style='background-color: #4CAF50; color: white; padding: 10px; text-align: center;'>")
.append(teamName).append("</h1>")
.append("<p style='font-size: 16px; line-height: 1.5;'>").append(description).append("</p>")
.append("<h2 style='color: #333; margin-top: 20px;'>Team Members</h2>")
.append("<ul style='list-style-type: none; padding: 0;'>");

for (TeamBuilding memberBuilding : teamMembers) {
Member member = memberBuilding.getMember();
emailBody.append(
"<li style='background-color: #f9f9f9; margin: 5px 0; padding: 10px; border-radius: 5px;'>")
.append("<strong>Name:</strong> ").append(member.getName()).append("<br>")
.append("<strong>Email:</strong> ").append(member.getEmail()).append("<br>")
.append("<strong>Position:</strong> ").append(memberBuilding.getMyPosition())
.append("</li>");
}
emailBody.append("</ul>")
.append("<p style='text-align: center; margin-top: 30px;'>")
.append("Thank you for using <strong>TeamUp</strong>")
.append("</p>")
.append("</div>");

for (TeamBuilding memberBuilding : teamMembers) {
emailService.sendMessage(memberBuilding.getMember().getEmail(), emailBody.toString());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spring:
host: ${MAIL_SERVER}
username: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}

logging:
level:
org.hibernate.sql: debug
Expand Down

0 comments on commit 1c64cab

Please sign in to comment.