HTML - PHP 联系表单电子邮件


HTML - PHP contact form email

我在这里和谷歌中搜索了如何制作联系表格来工作,但我无法。

我下载了一个"php联系表",但我不知道如何让它工作。我知道有很多关于这个问题的问题,但请帮助我。我被困在这里:/

这是 HTML 代码:

<form action="form-to-email.php" name="myemailform" method="post">
    <div>
        <span>Name</span>
        <input type="text" name="name" value="" placeholder="Your Name">
    </div>
    <div>
        <span>Email</span>
        <input type="email" name="email" autocapitalize="off" autocorrect="off" value="" placeholder="example@domain.com">
    </div>
    <div><textarea name="message" placeholder="Message"></textarea></div>
    <div class="code">
        <button><input type="submit" name='submit' value="Send"></button>
    </div>
    <i class="clear" style="display: block"></i>
    </div>
</form>

这是我从网站上下载的"免费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 = 'myEmail@domain.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 = "myEmail@domain.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;
  }
}
?> 

我需要在mysql中做一些事情还是做更多的事情?感谢您的帮助!

这里 "Here is the message:'n $message". <= 看到点了吗?这应该是一个分号。

这就是OP代码不起作用的原因。

将错误报告添加到文件顶部,这将有助于查找错误

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code

旁注:错误报告只应在暂存中完成,而不应在生产环境中完成。

一切看起来都正确,只需在重定向后添加退出即可。

header('Location: thank-you.html');
exit;