mailgun incoming mail event fetch attachment url

2019-07-10 17:12发布

问题:

I have a node endpoint that receives an incoming email in json, complete with any attachments from mailgun.

The attachments are in a json array (xxx.com is used for privacy)

attachments: '[{"url": "https://sw.api.mailgun.net/v3/domains/xxx.com/messages/eyJwIjpmYWxzZSwiayI6ImZhMTU0NDkwLWVmYzgtNDVlNi1hYWMyLTM4M2EwNDY1MjJlNCIsInMiOiI2NmU1NmMzNTIwIiwiYyI6InRhbmtiIn0=/attachments/0", "content-type": "image/png", "name": "ashfordchroming_logo.png", "size": 15667}]

But if i type the url in the browser:

https://sw.api.mailgun.net/v3/domains/xxx.com/messages/eyJwIjpmYWxzZSwiayI6ImZhMTU0NDkwLWVmYzgtNDVlNi1hYWMyLTM4M2EwNDY1MjJlNCIsInMiOiI2NmU1NmMzNTIwIiwiYyI6InRhbmtiIn0=/attachments/0

I get

{
  "message": "Domain not found: xxx.com"
}

I wanted the simplest way to show the image attachment in HTML, I was hoping the URL would just work since mailgun store the attachment.

So I was just trying to render the url in a template from Node.

Do I need to attach auth / API key credentials to the front of the URL to do this to test and make work?

回答1:

If you want to access the raw json, go to

https://sw.api.mailgun.net/v3/domains/xxx.com/messages/eyJwIjpmYWxzZSwiayI6ImZhMTU0NDkwLWVmYzgtNDVlNi1hYWMyLTM4M2EwNDY1MjJlNCIsInMiOiI2NmU1NmMzNTIwIiwiYyI6InRhbmtiIn0=/attachments/0

using username 'api' and password 'your-mailgun-privatekey'.

To do this programmatically, use the request package to read the buffer.

const rp = require("request-promise");
let file = rp.get({
  uri: "attachement-url",
  headers: {
    "Accept": "message/rfc2822"
  }
}).auth("api", "your private key")
/**Access the buffer here**/
file.on('data', (s => {
  console.log(s)
}))
file.pipe(fs.createWriteStream("./my-image.jpg"))

you can pipe the file to S3 or any cloud bucket.