Skip to content

Instantly share code, notes, and snippets.

@sairam
Created April 10, 2017 12:47
Show Gist options
  • Save sairam/73ff6c6f28d926cd5d3080eb7a004c57 to your computer and use it in GitHub Desktop.
Save sairam/73ff6c6f28d926cd5d3080eb7a004c57 to your computer and use it in GitHub Desktop.
Fetches logs/events from Mailgun based on event type like 'failure'
# Script to fetch failed records from mailgun
# see https://documentation.mailgun.com/api-events.html to customize
require 'rest-client'
require 'json'
api_key = "key-abcdefabcdef" # get your API key from https://app.mailgun.com/app/account/security
domain_name = "mg.example.com" # fill in your domain from your mailgun account
def get_data(url, send_params, events)
puts "fetching from url #{url}"
raw_data = if !send_params
RestClient.get url
else
RestClient.get url,
:params => {
:"event" => events
}
end
parsed = JSON.parse(raw_data)
items = parsed['items'].map do |item|
[item['recipient'], item['timestamp'].to_i, item['recipient-domain'], item['severity'], item['event'], item['message']['headers']['subject']]
end
[items, parsed['paging']['next']]
end
events = 'rejected OR failed'
extracted_data = []
url = "https://api:#{api_key}@api.mailgun.net/v3/#{domain_name}/events"
nurl = url
first_time = true
prev_url = ""
10.times do |i|
puts "In loop #{i+1}"
data, next_url = get_data(nurl, first_time, events)
extracted_data << data.map{|x| x.join(",")}
if prev_url == next_url
puts "Reached the last possible url"
break
end
prev_url = next_url
nurl = next_url.sub('api.mailgun.net',"api:#{api_key}@api.mailgun.net")
first_time = false
end
f = open('mailgundata.csv', 'w')
f.write(extracted_data.join("\n"))
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment