How To View Your Messages with Mailgun

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:





alt

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 to view the parsed message:





alt

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 :)



"""View a message using it’s Mailgun storage key."""
import os  
import sys

import requests

if len(sys.argv) != 2:  
    print "Usage: retrieve.py message_key"
    sys.exit(1)

# keep it in secret!
api_key = "key-...."

# output file name

file_name = "message.eml"

# let's make the url for retrieval
domain = "mailgun.com"  
key = sys.argv[1]  
url = "https://api.mailgun.net/v3/domains/%s/messages/%s"  
url = url % (domain, key)

# this will help us to get the raw MIME
headers = {"Accept": "message/rfc2822"}

# let's make a request to the API
r = requests.get(url, auth=("api", api_key), headers=headers)

if r.status_code == 200:  
    # dump the body to a file
    with open(file_name, "w") as message:
        message.write(r.json()["body-mime"])
    # open it in the bird
    os.system("thunderbird -file %s" % file_name)
else:  
    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.




alt

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!


comments powered by Disqus

Mailgun Stay in Touch

Get new posts delivered straight to your inbox.