Skip to content

Commit 1378ccc

Browse files
author
Chris Wilson
committed
1 parent 0ad146b commit 1378ccc

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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.IRestConnection;
23+
import com.sparkpost.transport.RestConnection;
24+
25+
public class SendEmailTemplateSample extends SparkPostBaseApp {
26+
27+
static final Logger logger = Logger.getLogger(CreateTemplateSimple.class);
28+
29+
private Client client;
30+
31+
public static void main(String[] args) throws SparkPostException, IOException {
32+
Logger.getRootLogger().setLevel(Level.DEBUG);
33+
34+
SendEmailTemplateSample sample = new SendEmailTemplateSample();
35+
sample.runApp();
36+
}
37+
38+
private void runApp() throws SparkPostException, IOException {
39+
this.client = this.newConfiguredClient();
40+
41+
// Loads an email to send from the file system
42+
String fromAddress = getFromAddress();
43+
String[] recipients = getTestRecipients();
44+
45+
sendEmail(fromAddress, recipients);
46+
47+
}
48+
49+
private void sendEmail(String from, String[] recipients) throws SparkPostException {
50+
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
51+
52+
// Populate Recipients
53+
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
54+
for (String recipient : recipients) {
55+
RecipientAttributes recipientAttribs = new RecipientAttributes();
56+
recipientAttribs.setAddress(new AddressAttributes(recipient));
57+
recipientArray.add(recipientAttribs);
58+
}
59+
transmission.setRecipientArray(recipientArray);
60+
61+
// Populate Substitution Data
62+
Map<String, Object> substitutionData = new HashMap<String, Object>();
63+
64+
substitutionData.put("yourContent", "You can add substitution data too.");
65+
transmission.setSubstitutionData(substitutionData);
66+
67+
// You can use Jackson, GSON or whatever you standard JSON decoding library is to
68+
// Build this structure.
69+
List<Map<String, String>> offers = new ArrayList<Map<String, String>>();
70+
for (int i = 0; i < 2; i++) {
71+
72+
Map<String, String> offer = new HashMap<String, String>();
73+
offer.put("description", "description value " + i);
74+
offer.put("discount", "discount " + i);
75+
offer.put("image", "image " + i);
76+
offer.put("image_announcer", "image_announcer " + i);
77+
offer.put("alt_title", "alt_title " + i);
78+
offer.put("tracking", "tracking " + i);
79+
offer.put("name", "name " + i);
80+
offer.put("id", "id " + i);
81+
offer.put("announcer_paid", "announcer_paid " + i);
82+
offer.put("announcer_image", "announcer_image " + i);
83+
offer.put("announcer_alt_title", "announcer_alt_title " + i);
84+
85+
offers.add(offer);
86+
}
87+
88+
substitutionData.put("offers", offers);
89+
90+
// Populate Email Body
91+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
92+
contentAttributes.setFrom(new AddressAttributes(from));
93+
contentAttributes.setSubject("☰ Your subject content here. {{yourContent}}");
94+
contentAttributes.setText("You could do it for text too. See https://www.sparkpost.com/blog/advanced-email-templates/ for an example");
95+
contentAttributes.setHtml(
96+
"<b>Your Data:</b><br>\n"
97+
+ "<table border='1'>\n"
98+
+ " <tr>\n"
99+
+ " <th>description</th>\n"
100+
+ " <th>discount</th>\n"
101+
+ " <th>image</th>\n"
102+
+ " <th>image_announcer</th>\n"
103+
+ " <th>alt_title</th>\n"
104+
+ " <th>tracking</th>\n"
105+
+ " <th>name</th>\n"
106+
+ " <th>id</th>\n"
107+
+ " <th>announcer_paid</th>\n"
108+
+ " <th>announcer_image</th>\n"
109+
+ " <th>announcer_alt_title</th>\n"
110+
+ " </tr>\n"
111+
+ " {{each offers}} \n"
112+
+ " <tr>\n"
113+
+ " <td> {{{offers.description}}} </td>\n"
114+
+ " <td> {{{offers.discount}}} </td>\n"
115+
+ " <td> {{{offers.image}}} </td>\n"
116+
+ " <td> {{{offers.image_announcer}}} </td>\n"
117+
+ " <td> {{{offers.alt_title}}} </td>\n"
118+
+ " <td> {{{offers.tracking}}} </td>\n"
119+
+ " <td> {{{offers.name}}} </td>\n"
120+
+ " <td> {{{offers.id}}} </td>\n"
121+
+ " <td> {{{offers.announcer_paid}}} </td>\n"
122+
+ " <td> {{{offers.announcer_image}}} </td>\n"
123+
+ " <td> {{{offers.announcer_alt_title}}} </td>\n"
124+
+ " </tr>\n"
125+
+ " {{ end }} \n"
126+
+ "</table>\n\n");
127+
transmission.setContentAttributes(contentAttributes);
128+
129+
transmission.setContentAttributes(contentAttributes);
130+
131+
// Send the Email
132+
IRestConnection connection = new RestConnection(this.client, getEndPoint());
133+
Response response = ResourceTransmissions.create(connection, 0, transmission);
134+
135+
logger.debug("Transmission Response: " + response);
136+
}
137+
138+
}

0 commit comments

Comments
 (0)