TDB TDB -3 years ago 141
HTTP Question

How to retrieve stored message in mailgun messages api for Rails app

I am using mailgun to convert incoming emails to post requests for my rails app, and that part is working fine, but I also want to be able to retrieve stored messages from the mailgun messages API, and am having trouble accomplishing that. The docs for their messages api is:

https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages

To make things easier I installed the RestClient gem in my rails app and have hacked around with it in rails console, but cannot get the full email object to return.

EDIT - I can make a successful get request to the stored message URL using the Postman application, using basic authorization with 'api' for username and my api key for password. But when I try to make this request using the rest-client gem, it gives me a

401 unauthorized
error. Here's my attempt from inside the console of my rails app.

url = 'https://se.api.mailgun.net/v3/domains/rest-of-the-url...'
user = 'api'
secret ='my-api-key'

RestClient::Request.execute method: :get, url: url, username: user, password: secret

Answer Source

Not sure why it wasn't working before, but for anyone who's curious, this method using rest-client gem seems to do the trick!

url = [url of stored email]
user = 'api'
secret ='my-api-key'

response = RestClient::Request.execute method: :get, url: url, user: user, password: secret
email = JSON.parse(response.body) # the email object!
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download