mailgun-js-boland
mailgun-js-boland copied to clipboard
Can't read stored messages
Hi, I'm using the version 0.11.2 downloaded from NPM tonight. Trying to use the mailgun.messages(<message_key>).info() feature, and it doesn't work for me, no matter what I do.
I tried placing instead of <message_key> a string with the url, tried a string with just the key, and nothing provides me with the content of the message stored. When trying with regular curl request, I do get the message. Any idea what am I doing wrong?
Is it possible to get a demonstration of how to use it?
Thanks, Ido
I had this same issue. Everything worked with CURL but not with this module.
The problem turns out that the host for the stored messages is different than the rest of the API. If you do it like the example in the read me, then everything else works but this breaks.
To fix you can pass in the storage host to the constructor:
const mailgun = require('mailgun-js')({host: 'sw.api.mailgun.net', apiKey: api_key, domain: domain});
mailgun.messages(mail.storage.key).info((err, data) => {
console.log(data);
});
Then it works, but of course you need to re-construct the default one to do anything else.
Hmm thanks for the comment. This seems like a sensible workaround... What is this base url? This does not seem to be documented in the Mailgun documentation? At least I can't find it. Can you elaborate some more on this solution?
Yeah I don't see anything in the documentation mentioning the base URL explicitly. https://documentation.mailgun.com/en/latest/api-sending.html#retrieving-stored-messages mentions that you have to use the URL you get when you fetch the mail from the event. So I tried that URL via curl and it worked, and that's when I noticed the hosts were different.
Hi,
@cwilsontv - Thanks a lot for your reply! So my storage url is something like this: https://se.api.mailgun.net/v3/domains/MYDOMAIN/messages/THEKEY=';
I tried running the following code: var mailgun = require('mailgun-js')({ host: "se.api.mailgun.net", apiKey: apiKey, domain: domain }); mailgun.messages('THEKEY=').info((err, data) => { console.log(data); });;
Running it I get: { message: 'Message not found' }
What am I doing wrong?
ofcourse curl'ing to it, does work.
Did you try the CURL at the same time as your node script? Only thing I can think of is that the message worked before with the CURL test, but was gone when you hit it with node. Mailgun only keeps the messages for 3 days.
You get a different error if you hit the wrong host: Wrong host:
curl -s --user 'api:key-' -G https://api.mailgun.net/v3/domains/domain.com/messages/key=
{"error":"not found"}
Correct host with message older than 3 days:
curl -s --user 'api:key-' -G https://sw.api.mailgun.net/v3/domains/domain.com/messages/key=
{
"message": "Message not found"
}
So it looks like you're on the right track because you get the same error for an old message.
Here's the full script I was using. Our problem was a configuration error on our internal mail server caused mailgun to fail some incoming emails, so I wrote this to grab the messages and dump them to console so I could check to ensure nothing important was lost:
'use strict';
const moment = require('moment');
const Enumerable = require('linq-es2015');
const api_key = 'key-';
const domain = '';
const start_date = '2017-06-25';
function filter_results(mails) {
const filtered = Enumerable.asEnumerable(mails.items)
.Where(x => x['recipient-domain'] === domain)
.ToArray();
return filtered;
}
function retry_mail(mails) {
const mailgun = require('mailgun-js')({host: 'sw.api.mailgun.net', apiKey: api_key, domain: domain});
for (const mail of mails) {
mailgun.messages(mail.storage.key).info((err, data) => {
console.log(err, data);
});
}
}
async function get_failed_mail() {
const mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
const failed_mails = await mailgun.get(
`/${domain}/events?event=failed&begin=${moment(start_date).format('X')}&ascending=yes&severity=permanent`
);
retry_mail(filter_results(failed_mails));
}
get_failed_mail();
When I ran the script I noticed the oldest emails all gave me the "Message not found" that you are getting, so it's possible they are just too old?
You can try adjusting the date and only pulling recent emails to verify if this is the issue.
Hi, I tried it with Curl and it did work.
I wanted to make a service to get the emails by POST from Mailgun, and then parse the message received (which was stored). Is there another way of getting the stored message using POST from Mailgun (maybe by using the events, but only get those which I didn't check for already).
Thanks, Ido
I run into this issue as well. In my logs, I can see some emails being stored under the sw.api.mailgun.net and some under se.api.mailgun.net 🤔. Getting back to good old axios to handle message fetching.
axios.get(event.storage.url, {
auth: {
username: 'api',
password: <apiKey>
}
}).then(({ data: mail }) => {
console.log(mail)
}, err => cb(err))