1

What I am tried to do is to have a lambda function proccess emails forwarded by mailgun.

So far, I have setup mailgun's route so it will forward emails to a AWS api gateway, then the api gateway triggers a lambda function.

The problem comes when I try to process the mail, instead of getting a pretty Json that I am expecting inside the lambda's event.body, I m getting raw post form data like

--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 ......

My question is, what should I do to get the JSON version of the forwarded emails in lambda?

chrki
  • 6,143
  • 6
  • 35
  • 55
ChenL
  • 1,032
  • 1
  • 10
  • 18
  • Why are you expecting "pretty Json"? You're getting an email message, originally specified in RFC 822. What programming language are you using? There are libraries in most languages to parse RFC822 type of messages. – stdunbar Dec 20 '16 at 22:01
  • Im using node for lambda, from what i understand, mailgun parse the actual email it received and generate a JSON string containing the content of said email then forward it with POST request, therefore I am expecting a JSON – ChenL Dec 21 '16 at 00:02

2 Answers2

1

Not sure if you ever came to a solution, but I have this working with the following settings.

  1. Setup your API Gateway method to use "Use Lambda Proxy integration"
  2. In your lambda (I use node.js) use busboy to work through the multi-part submission from mailgun. (use this post for help with busboy Busboy help)
  3. Make sure that any code you are going to execute after all busboy is complete is executed in the 'finish' portion of the busboy code.
Greg Fennell
  • 176
  • 1
  • 12
0

This suggests that your mailgun route is misconfigured and ends with a MIME request:

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

From Receiving Messages via HTTP through a forward() action

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Guildencrantz
  • 1,875
  • 1
  • 16
  • 30
  • Thanks for your reply and apologies for the late reply,I m configuring via mail-guns website that only give me a limited list of options. To use Raw MIME, according to the official Docs, you will need to forward/send the requests to a endpoint with 'mime' at the end of the url, which is not the case for me, so it should have forwarded as fully parsed messages. – ChenL Jan 03 '17 at 20:45
  • If you're not requesting MIME, but mailgun's sending you MIME, you need to contact mailgun support. – Guildencrantz Jan 03 '17 at 20:48
  • I have contacted mailgun support, apparently a college of mine was able to decode the json directly in php, but with API_gateway and Lambda, its a different story, I suspect its how API gateway reformats the inbound request before sending it to lambda thats causing this. – ChenL Jan 09 '17 at 03:46