/
MailgunEmailClient.java
148 lines (136 loc) · 4.8 KB
/
MailgunEmailClient.java
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package formflow.library.email;
import static java.util.Collections.emptyList;
import com.mailgun.api.v3.MailgunMessagesApi;
import com.mailgun.client.MailgunClient;
import com.mailgun.model.message.Message;
import com.mailgun.model.message.MessageResponse;
import feign.FeignException;
import java.io.File;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class MailgunEmailClient implements EmailClient<MessageResponse> {
private String senderEmail;
private final String mailgunDomain;
private MailgunMessagesApi mailgunMessagesApi;
public MailgunEmailClient(@Value("${form-flow.email-client.mailgun.sender-email:}") String senderEmail,
@Value("${form-flow.email-client.mailgun.domain:}") String mailgunDomain,
@Value("${form-flow.email-client.mailgun.key:}") String mailgunKey
) {
this.senderEmail = senderEmail;
this.mailgunDomain = mailgunDomain;
this.mailgunMessagesApi = MailgunClient.config(mailgunKey)
.createApi(MailgunMessagesApi.class);
}
/**
* This sends an email with the least amount of information needed to be provided. Sets empty or defaults to the rest of the
* parameters.
*
* @param subject The subject line of the email
* @param recipientEmail The email address that will get the email, aka the To field
* @param emailBody The HTML version of the email body
* @return A Mailgun MessageResponse object
*/
@Override
public MessageResponse sendEmail(
String subject,
String recipientEmail,
String emailBody) {
return sendEmail(
subject,
recipientEmail,
emptyList(),
emptyList(),
emailBody,
emptyList(),
false);
}
/**
* This sends an email with the least amount of information needed to be provided, but with attachments. Sets empty or defaults
* to the rest of the parameters.
*
* @param subject The subject line of the email
* @param recipientEmail The email address that will get the email, aka the To field
* @param emailBody The HTML version of the email body
* @param attachments A list of files that will be added as attachments to the email
* @return A Mailgun MessageResponse object
*/
@Override
public MessageResponse sendEmail(
String subject,
String recipientEmail,
String emailBody,
List<File> attachments) {
return sendEmail(
subject,
recipientEmail,
emptyList(),
emptyList(),
emailBody,
attachments,
false);
}
/**
* The main method that sends to Mailgun. Allows all parameter customization.
*
* @param subject The subject line of the email
* @param recipientEmail The email address that will get the email, aka the To field
* @param emailsToCC A list of emails to be added into the CC field
* @param emailsToBCC A list of emails to be added into the BCC field
* @param emailBody The HTML version of the email body
* @param attachments A list of files that will be added as attachments to the email
* @param requireTls A way to make TLS required
* @return A Mailgun MessageResponse object
*/
@Override
public MessageResponse sendEmail(
String subject,
String recipientEmail,
List<String> emailsToCC,
List<String> emailsToBCC,
String emailBody,
List<File> attachments,
boolean requireTls) {
Message.MessageBuilder message = Message.builder()
.from(senderEmail)
.to(recipientEmail)
.subject(subject)
.html(emailBody)
.requireTls(requireTls);
if (emailsToCC != null && !emailsToCC.isEmpty()) {
message.cc(emailsToCC);
}
if (emailsToBCC != null && !emailsToBCC.isEmpty()) {
message.bcc(emailsToBCC);
}
if (attachments != null && !attachments.isEmpty()) {
message.attachment(attachments);
}
Message builtMessage = message.build();
try {
return mailgunMessagesApi.sendMessage(mailgunDomain, builtMessage);
} catch (FeignException exception) {
log.error(exception.getMessage());
}
return null;
}
/**
* This setter allows us to replace mailgunMessageApi with a mock for testing.
*
* @param mailgunMessageApi The mailgunMessageApi you want to use to interface with Mailgun.
*/
public void setMailgunMessagesApi(MailgunMessagesApi mailgunMessageApi) {
this.mailgunMessagesApi = mailgunMessageApi;
}
/**
* This setter allows you to change the senderEmail. By default, senderEmail is defined in application.yaml.
*
* @param senderEmail The email that is used to fill the from field.
*/
public void setSenderEmail(String senderEmail) {
this.senderEmail = senderEmail;
}
}