PHP邮件表单不起作用,除非我使用CAPTCHA

时间:2013-03-22 14:24:37

标签: php email html-form

我正在尝试将此表单发送到我的电子邮件地址。

这是我正在使用的PHP(它位于http://protoolstutorial.org/cottageinn/上的表单)。我需要知道为什么这不会发生并发送到我的电子邮件。我在代码中遗漏了什么?

<?php 

$your_email ='cottageinndrive@gmail.com';// <<=== update to your email address

session_start();

$errors = '';
$south = '';
$east = '';
$west = '';
$midwest = '';
$visitor_email = '';

if(isset($_POST['submit']))
{
    $south = $_POST['south'];
    $east = $_POST['east'];
    $west = $_POST['west'];
    $midwest = $_POST['midwest'];
    $visitor_email = $_POST['email'];

    ///------------Do Validations-------------

    if(empty($visitor_email))
    {
        $errors .= "\n Email is required fields. "; 
    }

    if(IsInjected($visitor_email))
    {
        $errors .= "\n Bad email value!";
    }

    if(empty($errors))
    {
        //send the email
        $to = $your_email;
        $subject="New form submission";
        $from = $your_email;
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
        $body = "A user  submitted the contact form:\n".
                "South: $south\n".
                "East: $east\n".
                "West: $west\n".
                "Midwest: $midwest\n".
                "Email: $visitor_email \n".
                "IP: $ip\n";    
        $headers = "From: $from \r\n";
        $headers .= "Reply-To: $visitor_email \r\n";

        mail($to, $subject, $body,$headers);
        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;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

  1. 在php的开头添加error_reporting(E_ALL)以获取错误。
  2. 使用前声明函数。
  3. 要智能,安全,轻松地发送邮件,请尝试http://code.google.com/a/apache-extras.org/p/phpmailer/