Skip to content

Commit c0e66bd

Browse files
committed
Merge pull request #25 from SparkPost/feature/betterSubstitutionSupport
Fixing issue #24 `Substitution data as string`
2 parents 8f96b53 + 91f42b9 commit c0e66bd

File tree

18 files changed

+210
-25
lines changed

18 files changed

+210
-25
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
Use this library in Java applications to easily access the SparkPost Email API in your application.
1010

11+
## Version Compatibility Note
12+
13+
### Version 0.12 -> 0.13
14+
15+
Although we try to maintain library backward compatibility this migration may require some minor changes to your code. Substitution data was changed from `Map<String, String>` to `Map<String, Object>`. Most client code will just need to change their map to this new signature.
16+
1117
## Getting Started
1218

1319
The SparkPost Java Library is available in this [Maven Repository](http://maven.apache.org/download.cgi):
@@ -17,7 +23,7 @@ The SparkPost Java Library is available in this [Maven Repository](http://maven.
1723
<dependency>
1824
<groupId>com.sparkpost</groupId>
1925
<artifactId>sparkpost-lib</artifactId>
20-
<version>0.12</version>
26+
<version>0.13</version>
2127
</dependency>
2228
```
2329

apps/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>com.sparkpost</groupId>
55
<artifactId>sparkpost</artifactId>
6-
<version>0.12</version>
6+
<version>0.13</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99
<artifactId>apps</artifactId>

apps/sparkpost-documentor-app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.sparkpost</groupId>
66
<artifactId>apps</artifactId>
7-
<version>0.12</version>
7+
<version>0.13</version>
88
</parent>
99
<artifactId>sparkpost-documentor-app</artifactId>
1010
<name>Generates Markdown of Protocol</name>

apps/sparkpost-javamail-app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.sparkpost</groupId>
88
<artifactId>apps</artifactId>
9-
<version>0.12</version>
9+
<version>0.13</version>
1010
</parent>
1111
<groupId>com.sparkpost.sample</groupId>
1212
<artifactId>sparkpost-javamail-app</artifactId>

apps/sparkpost-javamail-app/src/main/java/com/sparkpost/sample/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private void sendEmail(String from, String[] recipients, String email) throws Sp
8181
transmission.setReturnPath(from);
8282

8383
// Populate Substitution Data
84-
Map<String, String> substitutionData = new HashMap<String, String>();
84+
Map<String, Object> substitutionData = new HashMap<String, Object>();
8585
substitutionData.put("from", from);
8686

8787
// SparkPost will set fields in HTML and/or Plain parts with the value here

apps/sparkpost-samples-app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.sparkpost</groupId>
66
<artifactId>apps</artifactId>
7-
<version>0.12</version>
7+
<version>0.13</version>
88
</parent>
99
<artifactId>sparkpost-samples-app</artifactId>
1010
<name>Example use SparkPost library</name>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
MIME-Version: 1.0
2+
Subject: {{subject}}
3+
To: {{address.email}}
4+
From: {{from}}
5+
Content-Type: multipart/alternative; boundary=001a113ed0b2fce06c052ecfc06d
6+
7+
--001a113ed0b2fce06c052ecfc06d
8+
Content-Type: text/plain; charset=UTF-8
9+
10+
Sample Email with Array Content
11+
12+
Name,\tValue
13+
{{each row_array}}
14+
{{loop_var.row}},\t {{loop_var.value}}
15+
{{end}}
16+
17+
18+
19+
End of array data
20+
21+
--001a113ed0b2fce06c052ecfc06d
22+
Content-Type: text/html; charset=UTF-8
23+
Content-Transfer-Encoding: quoted-printable
24+
25+
<div dir=3D"ltr">
26+
<div class=3D"gmail_signature">
27+
<div dir=3D"ltr">
28+
<div>
29+
<div>
30+
<h1 id=3D"toc_0" style=3D"margin-right:0px;margin-bottom:10px;margin-left:0px;padding:0px;font-size:28px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;margin-top:0px!important">Sample Email with Array Content</h1>
31+
<table style=3D"margin:15px 0px;padding:0px;border-collapse:collapse;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px">
32+
<thead>
33+
<tr style=3D"border-top-width:1px;border-top-style:solid;border-top-color:rgb(204,204,204);margin:0px;padding:0px">
34+
<th style=3D"border:1px solid rgb(204,204,204);margin:0px;padding:6px 13px">Name</th>
35+
<th style=3D"border:1px solid rgb(204,204,204);margin:0px;padding:6px 13px;text-align:center">Value</th>
36+
</tr>
37+
</thead>
38+
<tbody>
39+
{{each row_array}}
40+
<tr style=3D"border-top-width:1px;border-top-style:solid;border-top-color:rgb(204,204,204);margin:0px;padding:0px">
41+
<td style=3D"border:1px solid rgb(204,204,204);padding:6px 13px">{{loop_var.row}}</td>
42+
<td style=3D"border:1px solid rgb(204,204,204);padding:6px 13px;text-align:center">{{loop_var.value}}</td>
43+
</tr>
44+
{{end}}
45+
</tbody>
46+
</table>
47+
<p style=3D"margin-top:15px;margin-right:0px;margin-left:0px;color:rgb(0,0,0);font-family:Helvetica,arial,sans-serif;font-size:14px;margin-bottom:0px!important">End of array data</p>
48+
</div>
49+
</div>
50+
</div>
51+
</div>
52+
</div>
53+
54+
--001a113ed0b2fce06c052ecfc06d--

apps/sparkpost-samples-app/src/main/java/com/sparkpost/samples/SendEmailSample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void main(String[] args) throws SparkPostException, IOException {
3535
}
3636

3737
private void runApp() throws SparkPostException, IOException {
38-
client = this.newConfiguredClient();
38+
this.client = this.newConfiguredClient();
3939

4040
// Loads an email to send from the file system
4141
String template = getTemplate("sample_sp_email.eml");
@@ -61,7 +61,7 @@ private void sendEmail(String from, String[] recipients, String email) throws Sp
6161
transmission.setReturnPath(from);
6262

6363
// Populate Substitution Data
64-
Map<String, String> substitutionData = new HashMap<String, String>();
64+
Map<String, Object> substitutionData = new HashMap<String, Object>();
6565
substitutionData.put("from", from);
6666
transmission.setSubstitutionData(substitutionData);
6767

@@ -71,7 +71,7 @@ private void sendEmail(String from, String[] recipients, String email) throws Sp
7171
transmission.setContentAttributes(contentAttributes);
7272

7373
// Send the Email
74-
RestConnection connection = new RestConnection(client, getEndPoint());
74+
RestConnection connection = new RestConnection(this.client, getEndPoint());
7575
Response response = ResourceTransmissions.create(connection, 0, transmission);
7676

7777
logger.debug("Transmission Response: " + response);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
package com.sparkpost.samples;
3+
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.apache.log4j.Level;
11+
import org.apache.log4j.Logger;
12+
13+
import com.sparkpost.Client;
14+
import com.sparkpost.exception.SparkPostException;
15+
import com.sparkpost.model.AddressAttributes;
16+
import com.sparkpost.model.RecipientAttributes;
17+
import com.sparkpost.model.TemplateContentAttributes;
18+
import com.sparkpost.model.TransmissionWithRecipientArray;
19+
import com.sparkpost.model.responses.Response;
20+
import com.sparkpost.resources.ResourceTransmissions;
21+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
22+
import com.sparkpost.transport.RestConnection;
23+
24+
public class SendEmailWithSubstitutionSample extends SparkPostBaseApp {
25+
26+
static final Logger logger = Logger.getLogger(SendEmailWithSubstitutionSample.class);
27+
28+
private Client client;
29+
30+
public static void main(String[] args) throws SparkPostException, IOException {
31+
Logger.getRootLogger().setLevel(Level.DEBUG);
32+
33+
SendEmailWithSubstitutionSample sample = new SendEmailWithSubstitutionSample();
34+
sample.runApp();
35+
}
36+
37+
private void runApp() throws SparkPostException, IOException {
38+
this.client = this.newConfiguredClient();
39+
40+
// Loads an email to send from the file system
41+
String template = getTemplate("sample_sp_substitution_email.eml");
42+
String fromAddress = getFromAddress();
43+
String[] recipients = getTestRecipients();
44+
45+
sendEmail(fromAddress, recipients, template);
46+
47+
}
48+
49+
private void sendEmail(String from, String[] recipients, String email) throws SparkPostException {
50+
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
51+
52+
// Populate Recipients
53+
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
54+
55+
for (String recipient : recipients) {
56+
RecipientAttributes recipientAttribs = new RecipientAttributes();
57+
recipientAttribs.setAddress(new AddressAttributes(recipient));
58+
recipientArray.add(recipientAttribs);
59+
60+
Map<String, Object> substitution = new HashMap<String, Object>();
61+
recipientAttribs.setSubstitutionData(substitution);
62+
substitution.put("my_string", "This is a string value");
63+
64+
// Demonstrate dynamic subject per recipient. The subject contains "{{subject}}"
65+
substitution.put("subject", "A dynamic subject for " + "\"" + recipient + "\"");
66+
67+
/*
68+
* Demonstrate array of substitution data that is used in plain and HTML parts
69+
* Email body will contain:
70+
* {{each row_array}}
71+
* {{loop_var.row}},\t {{loop_var.value}}
72+
* {{end}}
73+
*/
74+
List<Map<String, String>> myArray = new ArrayList<Map<String, String>>();
75+
for (int i = 0; i < 10; i++) {
76+
Map<String, String> myMap = new HashMap<String, String>();
77+
myMap.put("row", "row " + (i + 1));
78+
myMap.put("value", "Value " + (i + 1));
79+
myArray.add(myMap);
80+
}
81+
substitution.put("row_array", myArray);
82+
}
83+
84+
transmission.setRecipientArray(recipientArray);
85+
86+
transmission.setReturnPath(from);
87+
88+
// Populate Substitution Data
89+
Map<String, Object> substitutionData = new HashMap<String, Object>();
90+
substitutionData.put("from", from);
91+
transmission.setSubstitutionData(substitutionData);
92+
93+
// Populate Email Body
94+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
95+
contentAttributes.setEmailRFC822(email);
96+
transmission.setContentAttributes(contentAttributes);
97+
98+
// Send the Email
99+
RestConnection connection = new RestConnection(this.client, getEndPoint());
100+
Response response = ResourceTransmissions.create(connection, 0, transmission);
101+
102+
logger.debug("Transmission Response: " + response);
103+
}
104+
105+
}

libs/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>com.sparkpost</groupId>
66
<artifactId>sparkpost</artifactId>
7-
<version>0.12</version>
7+
<version>0.13</version>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<artifactId>libs</artifactId>

0 commit comments

Comments
 (0)