我正在建立一个网站,就像标题所说的那样,我有一些关于html的问题应该发布到一个php文件,将表单输入通过电子邮件发送到我的邮箱。 看起来这是一个常见的错误,但我找不到适用于我的可靠解决方案。当我按下提交时,我得到405不允许的nginx类型的错误。 在我发布代码之前,我应该添加我在GitHub页面上托管用于测试目的,这可能是我收到此错误的原因吗? 当我在本地测试,使用MAMP并尝试提交时,浏览器(Chrome)以纯文本格式打开php文件。
这是我的html表单:
<div id="form">
<form method="post" name="my_message_form" action="form-to-email.php">
<p id="address_title">address</p>
<input type="email" placeholder="your email address" name="sender_email">
<p id="message_title">message</p>
<textarea placeholder="type your message here..." rows="5" cols="80" name="message"></textarea>
<input type="submit" name="send_button" value="send">
</form>
</div>
这是我的php文件:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
header('Location: error_form.html');
}
$visitor_email = $_POST['sender_email'];
$message = $_POST['message'];
//Validate first
if(empty($visitor_email))
{
echo "I need your email to reply to you!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'heuze.maxime@gmail.com';
$email_subject = "New message from your website";
$email_body = "You have received a new message from $visitor_email.\n".
"Here is the message:\n $message".
$to = "heuze.maxime@gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
如果有人能够弄清楚什么是错的,那么这对我的项目有很大的帮助,谢谢你的研究!