Skip to content

Commit d8bf470

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 7e906a0 + 6295747 commit d8bf470

File tree

3 files changed

+237
-0
lines changed

3 files changed

+237
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
email,reason,detail,created_at,expires_at,last_event_at,expires_at,subaccount
2+
00000000@EXAMPLE.com,soft-bounce,,"2015-11-03 10:22:19.9567","2015-11-04 10:22:19","2015-11-03 10:22:19.95665","2015-11-04 10:22:19",
3+
01904758111@example.COM,hard-bounce,"SMTP; 550 5.4.1 [01904758111@EXAMPLE.COM]: Recipient address rejected: Access denied","2016-01-14 19:08:17.51258","2016-01-21 19:08:17","2016-01-14 19:08:17.51253","2016-01-21 19:08:17",
4+
01906204000@EXANPLE.COM,hard-bounce,"smtp;550 5.1.10 RESOLVER.ADR.RecipientNotFound; Recipient not found by SMTP address lookup","2015-12-18 17:49:49.74724","2016-01-28 19:44:39","2016-01-14 19:44:39.83638","2016-01-28 19:44:39",
5+
01906823929@EXAMPLE.COM,hard-bounce,"SMTP; 550 5.4.1 [01906823929@EXAMPLE.COM]: Recipient address rejected: Access denied","2016-02-11 21:13:03.00523","2016-02-18 21:13:03","2016-02-11 21:13:03.00518","2016-02-18 21:13:03",
6+
0uojuc1q5p@gmail.com,hard-bounce,"smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces. Learn more at https://support.google.com/mail/answer/6596 c188si8742007ywf.98 - gsmtp","2016-03-29 16:37:24.59657","2016-04-05 16:37:24","2016-03-29 16:37:24.59654","2016-04-05 16:37:24",
7+
10687603@example.com.ar,soft-bounce,,"2015-06-05 19:50:18.09675","2015-06-11 21:04:00","2015-06-10 21:04:00.63428","2015-06-11 21:04:00",
8+
10742937@example.com.ar,soft-bounce,,"2015-06-09 18:17:29.87352","2015-06-10 18:17:29","2015-06-09 18:17:29.87349","2015-06-10 18:17:29",
9+
10803057@example.com.ar,soft-bounce,,"2015-05-19 20:50:11.78634","2015-06-09 19:28:15","2015-06-05 19:28:15.04113","2015-06-09 19:28:15",
10+
10835434@example.com.ar,soft-bounce,,"2015-05-15 18:27:48.64675","2015-05-16 18:27:48","2015-05-15 18:27:48.6467","2015-05-16 18:27:48",
11+
10835594@example.com.ar,soft-bounce,,"2015-06-09 14:10:20.86574","2015-06-11 14:21:03","2015-06-10 14:21:03.89673","2015-06-11 14:21:03",
12+
10835805@example.com.ar,soft-bounce,,"2015-05-19 21:42:09.13229","2015-05-28 18:55:13","2015-05-27 18:55:13.1885","2015-05-28 18:55:13",
13+
10835886@example.com.ar,soft-bounce,,"2015-05-19 14:16:12.35937","2015-05-23 13:26:26","2015-05-22 13:26:26.0547","2015-05-23 13:26:26",
14+
10847089@example.com.ar,soft-bounce,,"2015-06-09 19:04:32.1635","2015-06-10 19:04:32","2015-06-09 19:04:32.16347","2015-06-10 19:04:32",
15+
10849915@example.com.ar,soft-bounce,,"2015-06-09 22:37:12.14275","2015-06-10 22:37:12","2015-06-09 22:37:12.1427","2015-06-10 22:37:12",
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
package com.sparkpost.samples;
3+
4+
import java.io.BufferedReader;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.util.ArrayList;
9+
10+
import org.apache.commons.lang3.StringUtils;
11+
import org.apache.log4j.Level;
12+
import org.apache.log4j.Logger;
13+
14+
import com.sparkpost.Client;
15+
import com.sparkpost.exception.SparkPostException;
16+
import com.sparkpost.model.SuppressionList;
17+
import com.sparkpost.model.SuppressionListEntry;
18+
import com.sparkpost.model.responses.Response;
19+
import com.sparkpost.resources.ResourceSuppressionList;
20+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
21+
import com.sparkpost.transport.RestConnection;
22+
23+
/**
24+
* Lists all Sending Domains
25+
*/
26+
public class MandrillBlacklistImport extends SparkPostBaseApp {
27+
28+
static final Logger logger = Logger.getLogger(MandrillBlacklistImport.class);
29+
30+
private Client client;
31+
32+
public static class Fields {
33+
34+
public static final int EMAIL_COL = 0;
35+
public static final int REASON_COL = 1;
36+
public static final int DESCRIPTION_COL = 2;
37+
public static final int CREATED_AT_COL = 3;
38+
public static final int EXPIRES_AT_COL = 4;
39+
public static final int LAST_EVENT_AT_COL = 5;
40+
public static final int EXPIRES_AT2_COL = 6;
41+
public static final int SUBACCOUNT_COL = 7;// Unused
42+
43+
}
44+
45+
public static void main(String[] args) throws SparkPostException, IOException {
46+
Logger.getRootLogger().setLevel(Level.DEBUG);
47+
48+
MandrillBlacklistImport sample = new MandrillBlacklistImport();
49+
sample.runApp();
50+
}
51+
52+
/*
53+
* How to export Mandrill blacklist: https://mandrill.zendesk.com/hc/en-us/articles/205582997
54+
* Beware some samples I've received from Mandrill are not 100% correctly formated. I've found it necessary to remove some corrupt lines.
55+
*/
56+
private void runApp() throws SparkPostException, IOException {
57+
58+
this.client = this.newConfiguredClient();
59+
RestConnection connection = new RestConnection(this.client, getEndPoint());
60+
61+
SuppressionList suppressionList = new SuppressionList();
62+
suppressionList.setRecipients(new ArrayList<SuppressionListEntry>());
63+
64+
// Load sample Mandrill Blacklist file
65+
String csvFile = "samples/mandrillBlacklistExample.csv";
66+
BufferedReader br = null;
67+
String line = "";
68+
String cvsSplitBy = ",";
69+
70+
int row = 0;
71+
try {
72+
br = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile), "UTF-8"));
73+
74+
while ((line = br.readLine()) != null) {
75+
row++;
76+
if (StringUtils.isEmpty(line)) {
77+
System.out.println("Warning: LN:" + row + " is empty");
78+
continue;
79+
}
80+
String[] entryRow = line.split(cvsSplitBy);
81+
82+
if (entryRow.length < Fields.EXPIRES_AT2_COL) {
83+
System.out.println("Error: LN:" + row + " too short (" + entryRow.length + "),\n \"" + line + "\"\n");
84+
}
85+
86+
String email = entryRow[Fields.EMAIL_COL];
87+
System.out.println("Parse: " + line);
88+
89+
if (StringUtils.isEmpty(email)) {
90+
// Ignore rows that do not have an email address since it is required. Should we fail instead?
91+
System.out.println("Ignore header row");
92+
continue;
93+
} else if (email.equalsIgnoreCase("email")) {
94+
// First row is column names so skip it
95+
System.out.println("Warning: LN:" + row + " Ignore blank email row\"");
96+
continue;
97+
} else if (StringUtils.isEmpty(entryRow[Fields.REASON_COL]) || !entryRow[Fields.REASON_COL].equalsIgnoreCase("hard-bounce")) {
98+
// ignore empty reason or soft-bounces
99+
//System.out.println("Ignore reason: " + entryRow[Fields.REASON_COL]);
100+
System.out.println("LN:" + row + " Ignore reason: " + entryRow[Fields.REASON_COL]);
101+
continue;
102+
}
103+
104+
System.out.println("LN:" + row + " adding " + entryRow[Fields.EMAIL_COL]);
105+
106+
SuppressionListEntry entry = new SuppressionListEntry();
107+
// MBL: = Mandrill Blacklist
108+
entry.setDescription("MBL: " + line);
109+
entry.setEmail(entryRow[Fields.EMAIL_COL]);
110+
// Assumes Mandrill blacklist is only for non-transactional email
111+
entry.setTransactional(false);
112+
entry.setNonTransactional(true);
113+
114+
// Leave off source so it is set to "Manually Added"
115+
// entry.setSource(null);
116+
117+
// Maybe a Set is better to make sure addresses are unique
118+
suppressionList.getRecipients().add(entry);
119+
}
120+
121+
} finally {
122+
System.out.println("Last row: " + row);
123+
System.out.println("Last Line: " + line);
124+
if (br != null) {
125+
try {
126+
br.close();
127+
} catch (IOException e) {
128+
e.printStackTrace();
129+
}
130+
}
131+
}
132+
133+
System.out.println("Done");
134+
135+
if (suppressionList.getRecipients() == null || suppressionList.getRecipients().size() == 0) {
136+
System.out.println("There are no members of blacklist to add or update");
137+
return;
138+
139+
}
140+
Response result = ResourceSuppressionList.insertOrUpdateBulk(connection, suppressionList);
141+
System.out.println("Supression List Result: " + result);
142+
143+
}
144+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
package com.sparkpost.samples;
3+
4+
import java.io.IOException;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import org.apache.log4j.Level;
9+
import org.apache.log4j.Logger;
10+
11+
import com.sparkpost.Client;
12+
import com.sparkpost.exception.SparkPostException;
13+
import com.sparkpost.model.AddressAttributes;
14+
import com.sparkpost.model.StoredRecipientList;
15+
import com.sparkpost.model.TemplateContentAttributes;
16+
import com.sparkpost.model.TransmissionWithRecipientList;
17+
import com.sparkpost.model.responses.Response;
18+
import com.sparkpost.resources.ResourceTransmissions;
19+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
20+
import com.sparkpost.transport.RestConnection;
21+
22+
public class SendEmailRecipientListSample extends SparkPostBaseApp {
23+
24+
static final Logger logger = Logger.getLogger(CreateTemplateSimple.class);
25+
26+
private Client client;
27+
28+
public static void main(String[] args) throws SparkPostException, IOException {
29+
Logger.getRootLogger().setLevel(Level.DEBUG);
30+
31+
SendEmailRecipientListSample sample = new SendEmailRecipientListSample();
32+
sample.runApp();
33+
}
34+
35+
private void runApp() throws SparkPostException, IOException {
36+
this.client = this.newConfiguredClient();
37+
38+
// Loads an email to send from the file system
39+
String fromAddress = getFromAddress();
40+
String[] recipients = getTestRecipients();
41+
42+
sendEmail(fromAddress, recipients);
43+
44+
}
45+
46+
private void sendEmail(String from, String[] recipients) throws SparkPostException {
47+
48+
// CHANGE THIS TO ONE OF YOUR TEMPLATE IDs
49+
final String YOUR_RECIPIENT_LIST_ID = "30115044363775968";
50+
51+
TransmissionWithRecipientList transmission = new TransmissionWithRecipientList();
52+
StoredRecipientList recipiList = new StoredRecipientList();
53+
recipiList.setListId(YOUR_RECIPIENT_LIST_ID);
54+
transmission.setRecipientList(recipiList);
55+
56+
// Populate Substitution Data
57+
Map<String, Object> substitutionData = new HashMap<String, Object>();
58+
substitutionData.put("yourContent", "You can add substitution data too.");
59+
transmission.setSubstitutionData(substitutionData);
60+
61+
// Populate Email Body
62+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
63+
contentAttributes.setFrom(new AddressAttributes(from));
64+
contentAttributes.setSubject("Your subject content here. {{yourContent}}");
65+
contentAttributes.setText("Your Text content here. {{yourContent}}");
66+
contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{yourContent}}</p>");
67+
transmission.setContentAttributes(contentAttributes);
68+
69+
transmission.setContentAttributes(contentAttributes);
70+
71+
// Send the Email
72+
RestConnection connection = new RestConnection(this.client, getEndPoint());
73+
Response response = ResourceTransmissions.create(connection, 0, transmission);
74+
75+
logger.debug("Transmission Response: " + response);
76+
}
77+
78+
}

0 commit comments

Comments
 (0)