-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAwsSESUtils.java
More file actions
86 lines (72 loc) · 3.47 KB
/
AwsSESUtils.java
File metadata and controls
86 lines (72 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ceos.backend.infra.ses;
import ceos.backend.infra.ses.domain.EmailSendHistory;
import ceos.backend.infra.ses.domain.EmailType;
import ceos.backend.infra.ses.repository.EmailSendHistoryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;
import software.amazon.awssdk.services.ses.SesAsyncClient;
import software.amazon.awssdk.services.ses.model.*;
import java.util.concurrent.CompletableFuture;
@Slf4j
@Component
@RequiredArgsConstructor
public class AwsSESUtils {
private final SesAsyncClient sesAsyncClient;
private final SpringTemplateEngine templateEngine;
private final EmailSendHistoryRepository emailSendHistoryRepository;
public void singleEmailRequest(
String to, String subject, String template, Context context, EmailType emailType) {
final String html = templateEngine.process(template, context);
final SendEmailRequest.Builder sendEmailRequestBuilder = SendEmailRequest.builder();
sendEmailRequestBuilder.destination(Destination.builder().toAddresses(to).build());
SendEmailRequest request = sendEmailRequestBuilder
.message(newMessage(subject, html))
.source("ceos@ceos-sinchon.com")
.build();
CompletableFuture<SendEmailResponse> future = sesAsyncClient.sendEmail(request);
saveHistory(to, subject, template, emailType, future);
}
private Message newMessage(String subject, String html) {
final Content content = Content.builder().data(subject).build();
return Message.builder()
.subject(content)
.body(Body.builder().html(builder -> builder.data(html)).build())
.build();
}
private void saveHistory(String to, String subject, String template, EmailType emailType, CompletableFuture<SendEmailResponse> future) {
future.whenComplete(
(response, exception) -> {
if (exception != null) {
log.error("Failed to send email to: {}", to, exception);
saveFailureHistory(to, subject, template, emailType, exception);
} else {
log.info("Successfully sent email to: {}, messageId: {}", to, response.messageId());
saveSuccessHistory(to, subject, template, emailType, response.messageId());
}
});
}
private void saveSuccessHistory(
String to, String subject, String template, EmailType emailType, String messageId) {
try {
EmailSendHistory history =
EmailSendHistory.createSuccess(to, subject, template, emailType, messageId);
emailSendHistoryRepository.save(history);
} catch (Exception e) {
log.error("Failed to save email send success history", e);
}
}
private void saveFailureHistory(
String to, String subject, String template, EmailType emailType, Throwable exception) {
try {
EmailSendHistory history =
EmailSendHistory.createFailure(
to, subject, template, emailType, exception.getMessage());
emailSendHistoryRepository.save(history);
} catch (Exception e) {
log.error("Failed to save email send failure history", e);
}
}
}