Using AWS API Gateway to POST email via AWS SES

南楼画角 提交于 2020-01-13 11:37:09

问题


I'm setting up an API from an App which allows the App user to contact someone for follow-up, but hides the email address from the sender (and allows it to be changed without a new release of the App).

I've managed to setup an AWS API Gateway GET method which sends email directly via SES

https://...aws.com/em/send/en?body=The+email+is+here

Using a path override of

Action=SendEmail
&Source=source%40mydomain.org
&Destination.ToAddresses.member.1=follow.up%40anydomain.com
&Message.Subject.Data=Request+For+Followup
&Message.Body.Text.Data={body}

I would much prefer to use a POST method - but am really struggling to work out what should go in the Action parameter and how to create the mapping template - or even if it is possible to create an application/x-www-form-urlencoded request without having to resort to a lambda function - although the AWS documentation does include a $util.urlEncode() function.

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

Edit:

I am trying to use a POST method to AWS Gateway and a POST to SES with the contents in the request body, but it is just not clear to me how to create the mapping template and I can't find any examples for SES.

Testing it with the mapping template (as per the example in the documentation)

Source=source%40mydomain.org
&Destination.ToAddresses.member.1=follow.up%40anydomain.com
&Message.Subject.Data=Request+For+Followup
&Message.Body.Text.Data=The+message+goes+here

And a content type application/x-www-form-urlencoded AWS gateway gives the error:

{
"message": "Unsupported Media Type"
}

If I use a mapping template

{
"Action":"SendEmail",
"Source":"source@mydomain.org",
"Destination":{
    "ToAddresses":{
        "member":["follow.up@anydomain.com"]
        }
    },
"Message":{
    "Subject": {
        "Data":"Request For Followup"
        },
    "Body":{
        "Text":{
            "Data":"The message goes here"
            }
        }
    }
}

and a content type of application/json AWS gateway gives the error

{
  "Output": {
    "__type": "com.amazon.coral.service#UnknownOperationException",
    "message": null
  },
  "Version": "1.0"
}

回答1:


Your mapping template configuration must be in a POST format, not in the JSON, since SES service doesn't support json (https://docs.aws.amazon.com/ses/latest/DeveloperGuide/query-interface-examples.html) for example this config has worked out for me

Action=SendEmail&
Message.Body.Text.Data=Some+text&
Message.Subject.Data=New+message&
ReturnPath=some@mail.io
&Destination.ToAddresses.member.1=some@destination.com
&Source=your@sender.email

Also, your path override must be SendEmail only for this case



来源:https://stackoverflow.com/questions/41778555/using-aws-api-gateway-to-post-email-via-aws-ses

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