4

We are working at the integration of our email application with SparkPost. The only issue we have is getting bounce emails from SparkPost to exclude them from future mailings. Our application retrieves bounce emails directly from the mail server. When the user uses the SparkPost SMTP settings in our software, he cannot retrieve and process bounce emails because SparkPost does not forward bounce messages to the user's bounce email address.

Webhooks will not work for us because they pull data in real time only. If our software is turned to off when the bounce email comes, the bounce will not be caught and will be lost for our software as there is no way to retrieve it at a later time.

So, please, let me know if there is a way to get bounce emails from SparkPost through API or via email just like Amazon SES does. Amazon SES simply forwards bounce emails to the email address specified by the user in our application (Return email header field in the message header).

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Alex
  • 41
  • 1
  • 2

2 Answers2

7

If you cannot accept pushed data via HTTP like event webhooks or even our relay webhooks, the next best thing would be our Message Events API(https://www.sparkpost.com/api#/reference/message-events/message-events/search-for-message-events)

You could make a request to just get bounces for last hour like this:

https://api.sparkpost.com/api/v1/message-events?events=bounce,out_of_band

If you want more specific time ranges just add a from/to as well as a timezone if you need that:

https://api.sparkpost.com/api/v1/message-events?from=2015-09-10T00:00&to=2015-09-10T23:59&timezone=America/New_York

Bob Evans
  • 616
  • 6
  • 18
  • Hi Robert, Thank you for your reply. We made the request like this https://api.sparkpost.com/api/v1/message-events?events=bounce&from=2015-01-10T00:00&to=2015-09-10T23:59&timezone=America/New_York and got the response below though we have 5 bounces in our console: { "results": [], "total_count": 0, "links": [] } Please, let us know what we are doing wrong. – Alex Sep 14 '15 at 10:12
  • I can't see the query Params as it was truncated in your comment. That result means there were no events for the time range specified with your query Params. If you omit from and to, it defaults to the last hour – Bob Evans Sep 15 '15 at 12:22
  • 1
    Hi Robert, here is the full request: `https://api.sparkpost.com/api/v1/message-events?events=bounce&from=2015-01-10T00:00&to=2015-09-10T23:59&timezone=America/New_York` – Alex Sep 16 '15 at 08:39
  • So there are only 10 days worth of this data so the time range you have is valid but misleading. But you apparently have no bounces up until 9/10 at 11:59pm. There are two types of bounces "bounce" and "out_of_band". – Bob Evans Sep 18 '15 at 00:57
  • Hi @RobertEvans - are the `from` and `to` parameters always in UTC, or are they in the timezone specified in `timezone`? – Erics Sep 12 '16 at 13:04
  • the latter. the from/to are not time-zone aware. What makes it timezone aware is the query param "timezone" the default is UTC – Bob Evans Oct 06 '16 at 16:38
2

I wrote the following ruby code to get them as CSV:

require 'net/http'
require 'json'
require 'csv'

uri = URI('https://api.sparkpost.com/api/v1/message-events?events=bounce,out_of_band')
req = Net::HTTP::Get.new(uri)
req['Content-Type'] = 'application/json'
req['Authorization'] = ENV['API_KEY'] || raise('please provide API_KEY env variable')

res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |https|
  https.request(req)
end

bounces = JSON.parse(res.body)['results']
puts "#{bounces.count} bounces found"

CSV.open("bounces.csv", "wb") do |csv|
  csv << %w(Timestamp Recipient Reason)
  bounces.each do |bounce|
    csv << [bounce['timestamp'], bounce['rcpt_to'], bounce['reason']]
  end
end

Available as gist here: https://gist.github.com/schmijos/05d2f989c7a5854fe2cd31c666f61c39

schmijos
  • 8,114
  • 3
  • 50
  • 58