We are using mailgun and some of our users are placed on the suppression list. It can be tedious to remove multiple users at once so I want to do the same through the API. I also want to save some information regarding why the users email was bounced so I can relay it back to them. I'm doing this like so:
fs.readFile('emails.txt', 'utf8', function(err, data) {
if (err) throw err;
var emails = data.split(',');
async.eachSeries(emails, function(email, cb){
options.url = 'https://api.mailgun.net/v3/XXXXX.com/bounces/'+email
request.get(options, function(err, res, body){
body = JSON.parse(body);
error_code = body.error;
bounced_at = body.created_at;
var str = "\n Error code: "+body.error+" for email: "+email+" was bounced at: "+body.created_at
fs.appendFile("output.txt", str, function(err){
if(err) console.log(err)
console.log("Details were saved to output.txt")
})
});
request.del(options, function(err, res, body){
if(err){
cb(err);
} else {
console.log('Email: '+email+' has been unsuppressed');
}
})
cb(null);
}, function(err){
if(err){
console.log(err);
} else {
console.log('All bounced emails removed')
}
});
});
Below is a test with 5 users.
Input:
test1@ah.com,test2@ah.com,test3@ah.com,test4@ah.com,test5@ah.com
Output:
Error code: 4.4.4 for email: test4@ah.com was bounced at: Tue, 30 May 2017 11:40:17 UTC Error code: 3.3.3 for email: test3@ah.com was bounced at: Tue, 30 May 2017 11:40:05 UTC Error code: 2.2.2 for email: test2@ah.com was bounced at: Tue, 30 May 2017 11:39:57 UTC Error code: 1.1.1 for email: test1@ah.com was bounced at: Tue, 30 May 2017 11:39:45 UTC Error code: 5.5.5 for email: test5@ah.com was bounced at: Tue, 30 May 2017 11:40:27 UTC
We can see that these don't complete in order, which is fine. Sure this is enough in the async docs here.
Note, that since this function applies iteratee to each item in parallel, there is no guarantee that the iteratee functions will complete in order.
However, when I test with ten emails:
test1@ah.com,test2@ah.com,test3@ah.com,test4@ah.com,test5@ah.com,test6@ah.com,test7@ah.com,test7@ah.com,test8@ah.com,test9@ah.com,test10@ah.com
This is the output:
Error code: 2.2.2 for email: test2@ah.com was bounced at: Tue, 30 May 2017 11:43:57 UTC Error code: undefined for email: test10@ah.com was bounced at: undefined Error code: 1.1.1 for email: test1@ah.com was bounced at: Tue, 30 May 2017 11:43:50 UTC Error code: 8.8.8 for email: test8@ah.com was bounced at: Tue, 30 May 2017 11:44:43 UTC Error code: 3.3.3 for email: test3@ah.com was bounced at: Tue, 30 May 2017 11:44:05 UTC Error code: 6.6.6 for email: test6@ah.com was bounced at: Tue, 30 May 2017 11:44:28 UTC Error code: 5.5.5 for email: test5@ah.com was bounced at: Tue, 30 May 2017 11:44:21 UTC Error code: 4.4.4 for email: test4@ah.com was bounced at: Tue, 30 May 2017 11:44:13 UTC Error code: undefined for email: test7@ah.com was bounced at: undefined Error code: 9.9.9 for email: test9@ah.com was bounced at: Tue, 30 May 2017 11:44:55 UTC
It seems as though in some cases, my request.get()
was performed after the request.del()
-> this leads to the output log having Undefined
in some places. How can I ensure the request.get()
is always executed first?
via Someguywhocodes
No comments:
Post a Comment