Следующий код был написан на javascript.Прекрасно работает в AWS lambda и прекрасно работает при вызове из шлюза AWS Api (без искаженных сообщений):
var aws = require("aws-sdk");
var ses = new aws.SES({
accessKeyId: "Your SES Access Key",
secretAccesskey: "Your Secret key",
region: "us-east-1" // CHANGE with the region where you configured SES
});
exports.handler = function(event, context, callback) {
var requestPath = JSON.parse(JSON.stringify(event.pathParameters));
var requestBody = JSON.parse(event.body);
var responseBody = {};
var response = {};
if (
requestBody &&
requestBody.emailFrom &&
requestBody.subject &&
requestBody.htmlBody
) {
var emailTo = requestPath.emailto;
var emailFrom = requestBody.emailFrom;
var subject = requestBody.subject;
var htmlBody = requestBody.htmlBody;
} else {
responseBody = {
result: "fail",
resultCode: 400,
description:
"Incorrect Parameters. Mandatory: emailFrom, subject and bodyHTML"
};
response = {
statusCode: 400,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(responseBody),
isBase64Encoded: false
};
callback(null, response);
}
var emailParams = {
Destination: {
ToAddresses: [emailTo]
},
Message: {
Body: {
Html: {
Data: htmlBody
}
},
Subject: {
Data: subject
}
},
Source: emailFrom
};
var email = ses.sendEmail(emailParams, function(err, data) {
var resultCode = 200;
if (err) {
var responseBody = {
result: "FAIL",
resultCode: 500,
description: "Error sending email: " + err
};
resultCode = 500;
} else {
var responseBody = {
result: "OK",
resultCode: 200,
description: "Email sent successfully"
};
}
response = {
statusCode: resultCode,
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(responseBody),
isBase64Encoded: false
};
callback(null, response);
});
};