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: 메일 전송 구현 #18

Merged
merged 3 commits into from
Nov 2, 2024
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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-starter-common:2.1.0'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'

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

tasks.named('test') {
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/hackathon/momento/global/config/EmailConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.hackathon.momento.global.config;

import java.util.Properties;
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;

@Configuration
public class EmailConfig {

@Value("${spring.mail.host}")
private String host;

@Value("${spring.mail.port}")
private int port;

@Value("${spring.mail.username}")
private String username;

@Value("${spring.mail.password}")
private String password;

@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setPort(port);
mailSender.setHost(host);
mailSender.setUsername(username);
mailSender.setPassword(password);
mailSender.setDefaultEncoding("UTF-8");
mailSender.setJavaMailProperties(getMailProperties());

return mailSender;
}

private Properties getMailProperties() {
Properties properties = new Properties();
properties.put("mail.smtp.auth", true);
properties.put("mail.smtp.starttls.enable", true);
properties.put("mail.smtp.starttls.required", true);
properties.put("mail.smtp.connectiontimeout", 5000);
properties.put("mail.smtp.timeout", 5000);
properties.put("mail.smtp.writetimeout", 5000);

return properties;
}
}

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
6 changes: 6 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ spring:
port: ${REDIS_PORT}
host: ${REDIS_SERVER}

mail:
port: ${MAIL_PORT}
host: ${MAIL_SERVER}
username: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}

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