QuantumTiger
QuantumTiger

Reputation: 998

Using AWS API Gateway to POST email via AWS SES

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":"[email protected]",
"Destination":{
    "ToAddresses":{
        "member":["[email protected]"]
        }
    },
"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"
}

Upvotes: 4

Views: 4299

Answers (1)

VoVaVc
VoVaVc

Reputation: 839

Your mapping template configuration must be in a POST format, not in the JSON, since SES service doesn't support json. For example, this config has worked out for me:

Action=SendEmail&
Message.Body.Text.Data=Some+text&
Message.Subject.Data=New+message&
[email protected]
&[email protected]
&[email protected]

Also, your path override must be SendEmail only for the case above

Upvotes: 1

Related Questions