首页 > 解决方案 > 尝试使用 PHP 脚本将值从 HTML 表单发送到电子邮件,使用代码时收到错误 405

问题描述

因此,我正在尝试将我发现的代码示例集成到我的网页中,以将表单中的值直接发送到电子邮件,但是在使用本地主机时自行测试代码会给我一个错误 405

这是我正在使用的模板

PHP 脚本

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'unmovinglibrary@gmail.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".
    
$to = "unmovinglibrary@gmail.com";//<== update the email address
$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;
  }
}
   
?> 

html表单

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>PHP form to email sample form</title>
<!-- define some style elements-->
<style>
label,a 
{
    font-family : Arial, Helvetica, sans-serif;
    font-size : 12px; 
}

</style>    
<!-- a helper script for vaidating the form-->
<script language="JavaScript" src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>


<body>

<!-- Start code for the form-->
<form method="post" name="myemailform" action="form-to-email.php">
    <p>
        <label for='name'>Enter Name: </label><br>
        <input type="text" name="name">
    </p>
    <p>
        <label for='email'>Enter Email Address:</label><br>
        <input type="text" name="email">
    </p>
    <p>
        <label for='message'>Enter Message:</label> <br>
        <textarea name="message"></textarea>
    </p>
    <input type="submit" name='submit' value="submit">
</form>
<script language="JavaScript">
// Code for validating the form
// Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
// for details
var frmvalidator  = new Validator("myemailform");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("email","req","Please provide your email"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>
<p>
<a href='http://www.html-form-guide.com/email-form/php-form-to-email.html'
>PHP form to email article page</a>
</p>
</body>
</html>

我是否需要使用实际的实时托管才能使此代码正常工作?有没有其他方法可以用来将 HTML 表单值发送到电子邮件,因为我听说这种方法可能会将电子邮件推送到垃圾邮件

标签: phphtmlemail

解决方案


推荐阅读