Search code examples
pythonapirestmailgun

A way to view non-standard message headers after email sent with Mailgun?


I'm using Mailgun's RESTful API. I need a way to view non-standard headers of messages that have already been sent to connect them with a certain ID I have for database purposes. The main purpose of this is to log what emails are failing to be sent or not, along with the reasons and such.

My python code keeps returning a 405 response everytime I attempt to call something from "https://api.mailgun.net/v3/MY_DOMAIN_NAME/messages" when it is GET instead of POST. I am able to make other GET calls with events and stats as opposed to messages, so I do not think its the way I am writing the code exactly.

By calling on events I am able to view message status information, error codes, and ID's different from the ones I need. If I can just get this one non-standard header I will be set since it will give me the ID I need.

So, to the point, my question is if anyone who is familiar with this API knows if what I am trying to do is possible, and if so what should I look to be doing?

Here is an example of a call I make in case that helps:

This doesn't work:

def get_failed_events(start_date, end_date):
return requests.get(api_url.format('messages'), auth=('api', key), params={ 'begin': start_date, 'end': end_date}, verify=False)

This does:

def get_failed_events(start_date, end_date):
return requests.get(api_url.format('events'), auth=('api', key), params={ 'begin': start_date, 'end': end_date}, verify=False)

Solution

  • I found out that even though I was passing the information, I wasn't exactly passing it the way mailgun wants. It needs to be something like this c# example:

     var forMessageLog = "{\"x-header-id\": 1232}";
     message.Headers.Add("X-Mailgun-Variables", forMessageLog);
    

    Otherwise it will not appear in user variables.