-1

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

这是我正在使用的 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;
    }
}
?>
4

1 回答 1

0
  1. 在您的 php 开头添加error_reporting(E_ALL)以获取错误。
  2. 在使用它们之前声明函数。
  3. 对于智能、安全和轻松的邮件发送,请尝试http://code.google.com/a/apache-extras.org/p/phpmailer/
于 2013-03-22T14:32:40.657 回答