(NOOB in AWS.) Following is my .js code in my contact us page: Name, email and description are three fields I need for the email body.
function submitToAPI(e) {
e.preventDefault();
var name = document.getElementById("name-input").value;
var email = document.getElementById("email-input").value;
var desc = document.getElementById("description-input").value;
if (name=="" || email=="" || desc=="")
{
alert("Please Fill All Required Field");
return false;
}
var Name_n = /[A-Za-z]{1}[A-Za-z]/;
if (!Name_n.test($("name").val())) {
alert ("Not a valid name");
return;
}
if ($("email").val()=="")
{
alert ("Please enter your email id");
return;
}
var Email_E = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;
if (!Email_E.test($("email").val()))
{
alert ("Please enter valid email address");
return;
}
var data = {
name : name,
email : email,
desc : desc
};
$.ajax({
type: "POST",
url : "*AMAZON API Gateway URL*",
dataType: "json",
crossDomain: "true",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
success: function () {
// clear form and show a success message
alert("Thank you for your message. I will get back to you at the earliest");
document.getElementById("reused_form").reset();
location.reload();
},
error: function () {
// show an error message
alert("Failed to contact backend.");
}});
}
Lambda function:
var AWS = require('aws-sdk');
var ses = new AWS.SES();
var RECEIVER = 'test@test.com';
var SENDER = 'test@test.com';
var response = {
"isBase64Encoded": false,
"headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'},
"statusCode": 200,
"body": "{\"result\": \"Success.\"}"
};
exports.handler = function (event, context) {
console.log('Received event:', event);
console.log('Received name:', event.name);
console.log('Received name:', event.email);
console.log('Received name:', event.desc);
sendEmail(event, function (err, data) {
context.done(err, null);
});
};
function sendEmail (event, done) {
var params = {
Destination: {
ToAddresses: [
RECEIVER
]
},
Message: {
Body: {
Text: {
Data: 'name: ' + event.name + '\nemail: ' + event.email + '\ndesc: ' + event.desc,
Charset: 'UTF-8'
}
},
Subject: {
Data: 'Test subject: ' + event.name,
Charset: 'UTF-8'
}
},
Source: SENDER
};
ses.sendEmail(params, done);
}
Logs:
2020-10-17T22:40:15.618Z d0d4615b-8d2a-4406-a71b-031bac89724b INFO
Received event: {
version: '2.0',
routeKey: 'POST /contactus',
rawPath: '/prod/contactus',
rawQueryString: '',
headers: {
accept: 'application/json, text/javascript, */*; q=0.01',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-GB,en;q=0.9,en-US;q=0.8,hi;q=0.7',
'content-length': '77',
'content-type': 'application/json; charset=UTF-8',
host: 'host',
origin: 'http://url',
referer: 'http://url',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
'x-amzn-trace-id': 'Root=1-5f8b72cf-3d5e0c457647314914a429d6',
'x-forwarded-for': 'ip',
'x-forwarded-port': '443',
'x-forwarded-proto': 'https'
},
requestContext: {
accountId: '509405368663',
apiId: '0j2y9oea5i',
domainName: 'host',
domainPrefix: '0j2y9oea5i',
http: {
method: 'POST',
path: '/prod/contactus',
protocol: 'HTTP/1.1',
sourceIp: 'ip',
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'
},
requestId: 'Uk7gXi6IFiAEPBw=',
routeKey: 'POST /contactus',
stage: 'prod',
time: '17/Oct/2020:22:40:15 +0000',
timeEpoch: 1602974415086
},
body: '{"name":"Test","email":"test@gmail.com","desc":"Test message from HTML Form"}',
isBase64Encoded: false
}
Console log for name
2020-10-17T22:40:15.619Z d0d4615b-8d2a-4406-a71b-031bac89724b INFO
Received name: undefined
Problem:
I am receiving an email from Amazon SES but name, email and description are set as "undefined".
The sent payload is inside event.body
but it is a string. Use let data = JSON.parse(event.body)
to convert it to object and then you'll have access to the name
, email
and desc
properties vis dot notation (eg data.name
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.