I've created websites with php web forms before, but I'm launching out to try to use a Lambda / API Gateway / SES combination in AWS while launching a website from S3, in order to create a dynamic submission form. If you'd like to take a quick look at the submission form (and error), it's here: https://precious-gemstones.com/about.html
Here is my javascript, which I've stored at the root level in my S3 bucket:
function submitToAPI(e) {
e.preventDefault();
var URL = "https://ryj32uh9ki.execute-api.us-east-1.amazonaws.com/email/email";
var Namere = /[A-Za-z]{1}[A-Za-z]/;
if (!Namere.test($("#name-input").val())) {
alert ("Name must be at least 2 characters.");
return;
}
if ($("#email-input").val()=="") {
alert ("Please enter your email.");
return;
}
var reemail = /^([w-.]+@([w-]+.)+[w-]{2,6})?$/;
if (!reemail.test($("#email-input").val())) {
alert ("Please enter a valid email address.");
return;
}
var name = $("#name-input").val();
var email = $("#email-input").val();
var message = $("#message-input").val();
var data = {
name : name,
email : email,
message : message
};
$.ajax({
type: "POST",
url : "https://ryj32uh9ki.execute-api.us-east-1.amazonaws.com/email/email",
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 inquiry!");
document.getElementById("contact-form").reset();
location.reload();
},
error: function () {
// show an error message
alert("Error; please try again.");
}});
}
Here is my Lambda function:
var AWS = require('aws-sdk');
var ses = new AWS.SES();
var RECEIVER = '[email protected]';
var SENDER = '[email protected]';
var response = {
"isBase64Encoded": false,
"headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'precious-gemstones.com'},
"statusCode": 200,
"body": "{"result": "Success."}"
};
exports.handler = function (event, context) {
console.log('Received event:', event);
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 + '
email: ' + event.email + '
message: ' + event.message,
Charset: 'UTF-8'
}
},
Subject: {
Data: 'Gemstone inquiry: ' + event.name,
Charset: 'UTF-8'
}
},
Source: SENDER
};
ses.sendEmail(params, done);
}
And here is my html code snippet:
<div class="contact-form">
<form id="contact-form" method="post" action="https://ryj32uh9ki.execute-api.us-east-1.amazonaws.com/email/email">
<input name="name" type="text" class="form-control" placeholder="Your Name" required>
<br />
<input name="email" type="email" class="form-control" placeholder="Your Email" required>
<br />
<textarea name="message" class="form-control" required rows="10">Your Message</textarea>
<br />
<input type="submit" class="form-control submit" value="Send inquiry">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="submitToAPI.js"></script>
</body>
Any ideas?
Thanks so much for the help!
UPDATE: My API Gateway info as seen from my Lambda trigger is as follows:
API endpoint: https://ryj32uh9ki.execute-api.us-east-1.amazonaws.com/email/email
API type: REST
Authorization: NONE
Method: POST
Resource path: /email
Stage: email
No keys or authorization required, and the setup works (I receive the email) when I test from Lambda. The test from the API Gateway post method is also successful. However, it doesn't work from my site, which leads me to believe it's something in my html / javascript code, and I shall review / revise that tonight or tomorrow - but any insights are appreciated!
question from:
https://stackoverflow.com/questions/65557523/any-ideas-on-api-gateway-url-error-ses-api-gateway-lambda-integration-with-ja