Recently I have started using Amazon's AWS for web hosting and my HTML contact form does not seem to work anymore, comes back with this error when clicking on the submit button.
<html> <head><title>405 Method Not Allowed</title></head> <body> <h1>405 Method Not Allowed</h1> <ul> <li>Code: MethodNotAllowed</li> <li>Message: The specified method is not allowed against this resource.</li> <li>Method: POST</li> <li>ResourceType: OBJECT</li> <li>RequestId: A8750F9D0586C1E5</li> <li>HostId: FHjhs//WnJVj8GLE/hKVzkaIBq9DRuhNJxObJ8eARsvAbURpWS87tWivbIsCGnzAeFne9lLLvNI=</li> </ul> <hr/> </body> </html>
my html:
<div class="email-form">
<form id="ajax-contact" method="post" action="mailer.php">
<div class="flex-input-wrap">
<input type="text" placeholder="Name" name="name" id="name" class="flex-input-1" required="">
<input type="email" placeholder="Email" name="email" id="email" class="flex-input-2" required="">
</div>
<textarea type="text" placeholder="Enter text" name="message" id="message" required=""></textarea>
<button type="submit" class="submit-button">Send message</button>
</form>
<div id="form-messages"></div>
</div>
js:
$(function () {
var e = $("#ajax-contact"),
a = $("#form-messages");
$(e).submit(function (s) {
s.preventDefault();
var r = $(e).serialize();
$.ajax({
type: "POST",
url: $(e).attr("action"),
data: r
}).done(function (e) {
$(a).removeClass("error"), $(a).addClass("success"), $(a).text(e), $("#name").val(""), $("#email").val(""), $("#message").val("")
}).fail(function (e) {
$(a).removeClass("success"), $(a).addClass("error"), $(a).text("" !== e.responseText ? e.responseText : "Oops! An error occured and your message could not be sent.")
})
})
});
mailer.php file:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("","
"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo "Oops! Did you forget to fill out something? Try again.";
exit;
}
$recipient = "myemail@gmail.com";
$subject = "New email from $name";
$email_content = "Name: $name
";
$email_content .= "Email: $email
";
$email_content .= "Message:
$message
";
$email_headers = "From: $name <$email>";
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "Thanks! Your message was sent.";
} else {
http_response_code(500);
echo "Oops! Something went wrong and your message couldn't be sent.";
}
} else {
http_response_code(403);
echo "There was a problem with your message, please try again.";
}
?>
is there some type of setting that i'm missing in AWS?
thanks in advance.
Guessing that you have statically hosted your index.html in S3 and the POST is actually going to S3, which rejects it, rather than to your PHP server.