使用AWS Lambda处理mailgun的邮件时,收不到JSON格式的邮件

When use AWS Lambda to process mails from mailgun, can't get mails in JSON format

我试图做的是让 lambda 函数处理由 mailgun 转发的电子邮件。

到目前为止,我已经设置了 mailgun 的路由,因此它将电子邮件转发到 AWS api 网关,然后 api 网关触发 lambda 函数。

当我尝试处理邮件时出现问题,而不是在 lambda 的 event.body 中得到一个漂亮的 Json,我得到的是原始 post 表单数据喜欢

--cff4e6b3-a3a4-4131-bb8d-90a73f1b4c36\r\nContent-Disposition: form-data; name=\"Content-Type\"\r\n\r\nmultipart/mixed; boundary=\"001a1140216cee404d05440c49e7\"\r\n--cff4e6b3-a3a4-4131-bb8d-90a73f1b4c36\r\nContent-Disposition: form-data; name=\"Date\"\r\n\r\nTue, 20 Dec 2016 13:40:53 +1300\r\n--cff4e6b3-a3a4-4131-bb8d-90a73f1b4c36\r\nContent ......

我的问题是,我应该怎么做才能在 lambda 中获得 JSON 版本的转发邮件?

这表明您的 mailgun 路由配置错误并以 MIME 请求结束:

When you specify a URL of your application as a route destination through a forward() action, Mailgun will perform an HTTP POST request into it using one of two following formats:

Fully parsed: Mailgun will parse the message, transcode it into UTF-8 encoding, process attachments, and attempt to separate quoted parts from the actual message. This is the preferred option.

Raw MIME: message is posted as-is. In this case you are responsible for parsing MIME. To receive raw MIME messages, the destination URL must end with mime

来自Receiving Messages via HTTP through a forward() action

不确定您是否找到了解决方案,但我使用以下设置来解决这个问题。

  1. 设置您的 API 网关方法以使用 "Use Lambda Proxy integration"
  2. 在您的 lambda 中(我使用 node.js)使用 busboy 来完成来自 mailgun 的多部分提交。 (使用这个 post 来帮助服务员 Busboy help
  3. 确保在所有 busboy 完成后要执行的任何代码都在 busboy 代码的 'finish' 部分执行。