Skip to content

Commit 845c8e7

Browse files
committed
Adds example of importing Mandrill blacklist into SparkPost's Suppression List.
1 parent 6648c27 commit 845c8e7

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-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: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
package com.sparkpost.samples;
3+
4+
import java.io.BufferedReader;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
9+
import org.apache.commons.lang3.StringUtils;
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.SuppressionList;
16+
import com.sparkpost.model.SuppressionListEntry;
17+
import com.sparkpost.model.responses.Response;
18+
import com.sparkpost.resources.ResourceSuppressionList;
19+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
20+
import com.sparkpost.transport.RestConnection;
21+
22+
/**
23+
* Lists all Sending Domains
24+
*/
25+
public class MandrillBlacklistImport extends SparkPostBaseApp {
26+
27+
static final Logger logger = Logger.getLogger(MandrillBlacklistImport.class);
28+
29+
private Client client;
30+
31+
public static class Fields {
32+
33+
public static final int EMAIL_COL = 0;
34+
public static final int REASON_COL = 1;
35+
public static final int DESCRIPTION_COL = 2;
36+
public static final int CREATED_AT_COL = 3;
37+
public static final int EXPIRES_AT_COL = 4;
38+
public static final int LAST_EVENT_AT_COL = 5;
39+
public static final int EXPIRES_AT2_COL = 6;
40+
public static final int SUBACCOUNT_COL = 7;// Unused
41+
42+
}
43+
44+
public static void main(String[] args) throws SparkPostException, IOException {
45+
Logger.getRootLogger().setLevel(Level.DEBUG);
46+
47+
MandrillBlacklistImport sample = new MandrillBlacklistImport();
48+
sample.runApp();
49+
}
50+
51+
/*
52+
* How to export Mandrill blacklist: https://mandrill.zendesk.com/hc/en-us/articles/205582997
53+
* Beware some samples I've received from Mandrill are not 100% correctly formated. I've found it necessary to remove some corrupt lines.
54+
*/
55+
private void runApp() throws SparkPostException, IOException {
56+
57+
this.client = this.newConfiguredClient();
58+
RestConnection connection = new RestConnection(this.client, getEndPoint());
59+
60+
SuppressionList suppressionList = new SuppressionList();
61+
suppressionList.setRecipients(new ArrayList<SuppressionListEntry>());
62+
63+
// Load sample Mandrill Blacklist file
64+
String csvFile = "samples/mandrillBlacklistExample.csv";
65+
BufferedReader br = null;
66+
String line = "";
67+
String cvsSplitBy = ",";
68+
69+
int row = 0;
70+
try {
71+
br = new BufferedReader(new FileReader(csvFile));
72+
while ((line = br.readLine()) != null) {
73+
row++;
74+
if (StringUtils.isEmpty(line)) {
75+
System.out.println("Warning: LN:" + row + " is empty");
76+
continue;
77+
}
78+
String[] entryRow = line.split(cvsSplitBy);
79+
80+
if (entryRow.length < Fields.EXPIRES_AT2_COL) {
81+
System.out.println("Error: LN:" + row + " too short (" + entryRow.length + "),\n \"" + line + "\"\n");
82+
}
83+
84+
String email = entryRow[Fields.EMAIL_COL];
85+
System.out.println("Parse: " + line);
86+
87+
if (StringUtils.isEmpty(email)) {
88+
// Ignore rows that do not have an email address since it is required. Should we fail instead?
89+
System.out.println("Ignore header row");
90+
continue;
91+
} else if (email.equalsIgnoreCase("email")) {
92+
// First row is column names so skip it
93+
System.out.println("Warning: LN:" + row + " Ignore blank email row\"");
94+
continue;
95+
} else if (StringUtils.isEmpty(entryRow[Fields.REASON_COL]) || !entryRow[Fields.REASON_COL].equalsIgnoreCase("hard-bounce")) {
96+
// ignore empty reason or soft-bounces
97+
//System.out.println("Ignore reason: " + entryRow[Fields.REASON_COL]);
98+
System.out.println("LN:" + row + " Ignore reason: " + entryRow[Fields.REASON_COL]);
99+
continue;
100+
}
101+
102+
System.out.println("LN:" + row + " adding " + entryRow[Fields.EMAIL_COL]);
103+
104+
SuppressionListEntry entry = new SuppressionListEntry();
105+
// MBL: = Mandrill Blacklist
106+
entry.setDescription("MBL: " + line);
107+
entry.setEmail(entryRow[Fields.EMAIL_COL]);
108+
// Assumes Mandrill blacklist is only for non-transactional email
109+
entry.setTransactional(false);
110+
entry.setNonTransactional(true);
111+
112+
// Leave off source so it is set to "Manually Added"
113+
// entry.setSource(null);
114+
115+
// Maybe a Set is better to make sure addresses are unique
116+
suppressionList.getRecipients().add(entry);
117+
}
118+
119+
} finally {
120+
System.out.println("Last row: " + row);
121+
System.out.println("Last Line: " + line);
122+
if (br != null) {
123+
try {
124+
br.close();
125+
} catch (IOException e) {
126+
e.printStackTrace();
127+
}
128+
}
129+
}
130+
131+
System.out.println("Done");
132+
133+
if (suppressionList.getRecipients() == null || suppressionList.getRecipients().size() == 0) {
134+
System.out.println("There are no members of blacklist to add or update");
135+
return;
136+
137+
}
138+
Response result = ResourceSuppressionList.insertOrUpdateBulk(connection, suppressionList);
139+
System.out.println("Supression List Result: " + result);
140+
141+
}
142+
}

0 commit comments

Comments
 (0)