Skip to content

Commit ddd1d07

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # .github/workflows/ci.yml # pom.xml
2 parents cb19837 + 86bba50 commit ddd1d07

File tree

3 files changed

+224
-1
lines changed

3 files changed

+224
-1
lines changed

src/main/java/io/rocketbase/mail/PostmarkClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.rocketbase.mail.dto.EmailAttachment;
55
import io.rocketbase.mail.dto.Message;
66
import io.rocketbase.mail.dto.MessageResponse;
7+
import io.rocketbase.mail.dto.MessageWithTemplate;
78
import io.rocketbase.mail.util.MessageJsonWriter;
89
import lombok.RequiredArgsConstructor;
910
import org.springframework.core.ParameterizedTypeReference;
@@ -69,6 +70,15 @@ public List<MessageResponse> deliverMessage(List<Message> messages) {
6970
return response.getBody();
7071
}
7172

73+
public MessageResponse deliverMessageWithTemplate(MessageWithTemplate msg) {
74+
ResponseEntity<MessageResponse> response = getRestTemplate().exchange(createUriBuilder().path("/email/withTemplate")
75+
.toUriString(),
76+
HttpMethod.POST,
77+
new HttpEntity<>(msg, buildHeaders()),
78+
MessageResponse.class);
79+
return response.getBody();
80+
}
81+
7282
protected UriComponentsBuilder createUriBuilder() {
7383
return UriComponentsBuilder.fromUriString(postmarkProperties.getApi().getUrl());
7484
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package io.rocketbase.mail.dto;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.annotation.JsonSetter;
6+
import lombok.*;
7+
8+
import java.io.File;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.stream.Collectors;
15+
16+
@Data
17+
@AllArgsConstructor
18+
@NoArgsConstructor
19+
@Builder
20+
public class MessageWithTemplate {
21+
22+
private Long templateId;
23+
24+
private String templateAlias;
25+
26+
private Object templateModel;
27+
28+
private boolean inlineCss;
29+
30+
private String from;
31+
32+
private String to;
33+
34+
private String cc;
35+
36+
private String bcc;
37+
38+
private String replyTo;
39+
40+
private String tag;
41+
42+
private List<Header> headers;
43+
44+
private Boolean trackOpens;
45+
46+
private TrackLinksType trackLinks;
47+
48+
private Map<String, String> metadata;
49+
50+
51+
public MessageWithTemplate(String from, String to, Long templateId, Object templateModel) {
52+
setFrom(from);
53+
setTo(to);
54+
this.templateId = templateId;
55+
this.templateModel = templateModel;
56+
}
57+
58+
public MessageWithTemplate(EmailAddress from, EmailAddress to, String templateAlias, Object templateModel) {
59+
setFrom(from);
60+
setTo(to);
61+
this.templateAlias = templateAlias;
62+
this.templateModel = templateModel;
63+
}
64+
65+
@JsonCreator
66+
public MessageWithTemplate(@JsonProperty("from") String from,
67+
@JsonProperty("to") String to,
68+
@JsonProperty("templateId") Long templateId,
69+
@JsonProperty("templateAlias") String templateAlias,
70+
@JsonProperty("cc") String cc,
71+
@JsonProperty("bcc") String bcc) {
72+
setFrom(from);
73+
setTo(to);
74+
this.templateId = templateId;
75+
this.templateAlias = templateAlias;
76+
setCc(cc);
77+
setBcc(bcc);
78+
}
79+
80+
public void setFrom(String from) {
81+
this.from = from;
82+
}
83+
84+
public void setFrom(EmailAddress from) {
85+
this.from = from != null ? from.toRecipient() : null;
86+
}
87+
88+
public void setTo(EmailAddress to) {
89+
this.to = to != null ? to.toRecipient() : null;
90+
}
91+
92+
public void setTo(List<EmailAddress> to) {
93+
this.to = emailAddressList(to);
94+
}
95+
96+
public void setTo(String... to) {
97+
this.to = emailList(to);
98+
}
99+
100+
@JsonSetter
101+
public void setTo(String to) {
102+
this.to = to;
103+
}
104+
105+
public void setCc(EmailAddress cc) {
106+
this.cc = cc != null ? cc.toRecipient() : null;
107+
}
108+
109+
public void setCc(List<EmailAddress> cc) {
110+
this.cc = emailAddressList(cc);
111+
}
112+
113+
public void setCc(String... cc) {
114+
this.cc = emailList(cc);
115+
}
116+
117+
@JsonSetter
118+
public void setCc(String cc) {
119+
this.cc = cc;
120+
}
121+
122+
public void setBcc(EmailAddress bcc) {
123+
this.bcc = bcc != null ? bcc.toRecipient() : null;
124+
}
125+
126+
public void setBcc(List<EmailAddress> bcc) {
127+
this.bcc = emailAddressList(bcc);
128+
}
129+
130+
public void setBcc(EmailAddress... bcc) {
131+
this.bcc = emailList(bcc);
132+
}
133+
134+
public void setBcc(String... bcc) {
135+
this.bcc = emailList(bcc);
136+
}
137+
138+
@JsonSetter
139+
public void setBcc(String bcc) {
140+
this.bcc = bcc;
141+
}
142+
143+
@SneakyThrows
144+
protected byte[] readFileContent(String path) {
145+
return Files.readAllBytes(Paths.get(path));
146+
}
147+
148+
@SneakyThrows
149+
protected String readFileContentType(String path) {
150+
return Files.probeContentType(new File(path).toPath());
151+
}
152+
153+
public void setTemplateModel(Object templateModel) {
154+
this.templateModel = templateModel;
155+
}
156+
157+
public void setInlineCss(boolean inlineCss) {
158+
this.inlineCss = inlineCss;
159+
}
160+
161+
protected String emailAddressList(List<EmailAddress> addresses) {
162+
if (addresses == null || addresses.isEmpty()) {
163+
return null;
164+
}
165+
return addresses.stream()
166+
.map(EmailAddress::toRecipient)
167+
.collect(Collectors.joining(","));
168+
}
169+
170+
protected String emailList(String... addresses) {
171+
if (addresses == null || addresses.length == 0) {
172+
return null;
173+
}
174+
return Arrays.asList(addresses)
175+
.stream()
176+
.map(e -> new EmailAddress(e).toRecipient())
177+
.collect(Collectors.joining(","));
178+
}
179+
180+
protected String emailList(EmailAddress... addresses) {
181+
if (addresses == null || addresses.length == 0) {
182+
return null;
183+
}
184+
return Arrays.asList(addresses)
185+
.stream()
186+
.map(EmailAddress::toRecipient)
187+
.collect(Collectors.joining(","));
188+
}
189+
}

src/test/java/io/rocketbase/mail/PostmarkClientTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
import org.junit.jupiter.api.Disabled;
99
import org.junit.jupiter.api.Test;
1010

11-
1211
import java.io.File;
1312
import java.net.URL;
13+
import java.util.HashMap;
14+
import java.util.Map;
1415

1516
import static org.hamcrest.MatcherAssert.assertThat;
1617
import static org.hamcrest.Matchers.equalTo;
@@ -38,6 +39,29 @@ public void testDeliverMessage() {
3839
assertThat(response, notNullValue());
3940
}
4041

42+
43+
@Disabled
44+
@Test
45+
public void testDeliverMessageWithTemplate() {
46+
// given
47+
Map templateObject = new HashMap();
48+
templateObject.put("product_name", "RocketBase");
49+
templateObject.put("product_url", "https://www.rocketbase.io");
50+
templateObject.put("company_name", "RocketBase");
51+
templateObject.put("company_address", "Katharinenstraße 30a, 20457 Hamburg");
52+
templateObject.put("task_name", "task-name");
53+
templateObject.put("message", "Had some issues...\nplease take a look");
54+
55+
PostmarkClient client = new PostmarkClient(new PostmarkProperties("--"));
56+
MessageWithTemplate message = new MessageWithTemplate(new EmailAddress("info@rocketbase.io"),
57+
new EmailAddress("marten@rocketbase.io", "Marten Prieß"), "application-error", templateObject);
58+
59+
// when
60+
MessageResponse response = client.deliverMessageWithTemplate(message);
61+
// then
62+
assertThat(response, notNullValue());
63+
}
64+
4165
@Test
4266
public void testBuildJson() throws Exception {
4367
// given

0 commit comments

Comments
 (0)