• Email DIY

How To View Your Messages With Mailgun

Mailgun Team
5 min read

Recently, we began to receive a lot of requests from customers to have access to the content of their messages.

Internally, we store messages for 3 days for scheduled sends, troubleshooting and compliance verification. We’re happy to announce that we have exposed this feature in our events API!

You can see the API URL to the message along with its unique key under the “storage” section when expanding the log entry:



To view the message in the browser just copy the URL and enter “API” as username and your API key in the password box. This will let you view the parsed message:



Sometimes it’s handy to analyze the raw MIME and maybe render it in your favorite email client. Not a problem, here is an example on how you can do that with Thunderbird and a little bit of python magic 

1# View a message using it’s Mailgun storage key.
2import os
3import sys
4import requests
5
6if len(sys.argv) != 2:
7 print "Usage: retrieve.py message_key"
8 sys.exit(1)
9
10# keep it in secret!
11api_key = "key-...."
12
13# output file name
14file_name = "message.eml"
15
16# let's make the url for retrieval
17domain = "mailgun.com"
18key = sys.argv[1]
19url = "https://api.mailgun.net/v3/domains/%s/messages/%s"
20url = url % (domain, key)
21
22# this will help us to get the raw MIME
23headers = {"Accept": "message/rfc2822"}
24
25# let's make a request to the API
26r = requests.get(url, auth=("api", api_key), headers=headers)
27
28if r.status_code == 200:
29 # dump the body to a file
30 with open(file_name, "w") as message:
31 message.write(r.json()["body-mime"])
32 # open it in the bird
33 os.system("thunderbird -file %s" % file_name)
34else:
35 print "Oops! Something went wrong: %s" % r.content

When you run the script with the storage key as a parameter it will retrieve the message from Mailgun; save it to “message.eml” and open it in Thunderbird.

While we’re here we want to remind you that we made some improvements to our logs allowing you to view individual message history.

Happy message viewing!

Tags: Events

Last updated on September 16, 2019

  • Related posts
  • Recent posts
  • Top posts
View all

Always be in the know and grab free email resources!

No spam, ever. Only musings and writings from the Mailgun team.

Mailgun is committed to protecting your privacy. Please read ourPrivacy Policybefore providing us with your details.

sign up

It's easy to get started. And it's free.

What will you accomplish with 10,000 free emails and a 100 free validations every month?
Sign up for Free