Reputation: 1209
I'm working on some automated integration tests for an app using Mailgun. I'd like to use the Mailgun API to query for an email after sending it to confirm the contents match my expectations.
From the API documentation, it seems like there is no way to do a simple GET /messages
, and instead I should be using the Events API (see https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages)
The Events API is relatively straightforward and I'm able to filter results using a timestamp & recipient like so:
curl -s --user 'api:{MY_API_KEY_HERE}' -G \
"https://api.mailgun.net/v3/{MY_DOMAIN_HERE}/events" \
--data-urlencode begin=1606760326.939 \
--data-urlencode ascending=true \
--data-urlencode limit=5 \
--data-urlencode recipient={MY_EXPECTED_RECIPIENT_EMAIL_HERE}
In this example, the timestamp I'm using is Monday, November 30, 2020 6:18:46.939 PM UTC, and I should be getting up to 5 emails sent after that time. I sent a test email after that time and confirmed receipt in my test inbox, so I know that an email was sent.
However, when I issued that call at 6:18PM UTC, I received no results... and trying it again at 6:50PM UTC, still nothing. When I was doing similar testing over the weekend, I found almost zero delay between sending an email and then querying the events API for information...
It seems to me that the Events API has a significant delay and can't really be used for this type of use case (polling to confirm email has been sent). From the API docs, it does imply that this endpoint needs to be used carefully (see https://documentation.mailgun.com/en/latest/api-events.html#event-polling) but it's unclear if there is any kind of loose "guarantee" as to when events will actually appear.
Does anyone know how long these events are delayed?
Or, is there a better way for me to query the status of an individual email after sending it that doesn't rely on this API? I need to be able to check the most recent messages that have been enqueued and inspect the contents...
Upvotes: 1
Views: 971
Reputation: 401
There are no delay on MailGun side. The issue is API call parameters. Their API docs a little bit misleading I would say. There are two timestamps that you can pass to API. They are begin
and end
.
You are sending single begin
parameter with a timestamp. As I found in reality when you send single begin
parameter MaulGun returns all events before that timestamp that you have passed.
When you change begin
to end
and send it as single parameter to API then MailGun will return all events after that timestamp that you have passed.
And only in case when you send both begin
and end
parameters to API then MailGun returns all events that occurred between those begin
and end
timestamps that you have passed.
Upvotes: 2