mailgun incoming mail event fetch attachment url

妖精的绣舞 提交于 2019-12-12 19:08:55

问题


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.



来源:https://stackoverflow.com/questions/43872301/mailgun-incoming-mail-event-fetch-attachment-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!