PHP邮件表单不生成邮件

PHP邮件表单不生成邮件,php,forms,email,Php,Forms,Email,好的,我的php电子邮件脚本托管在我表单的另一台服务器上。直到最近,它一直运作良好,但一些不相关的变化似乎把事情搞砸了。我想我知道可能发生了什么,但让我先解释一下问题 起初我认为这是一个问题与去润滑!埃雷吉命令。所以,我把它们都改成了!赛前,但这显然不是唯一的问题 表单似乎正常工作,没有报告错误,成功页面正在解析,但从未发送过电子邮件(是的,我检查了垃圾邮件文件夹)。大约在问题开始的同一时间,我将原始php邮件脚本所在的网站和域移动到另一台服务器上(我无法在该服务器上托管php文件),但将php

好的,我的php电子邮件脚本托管在我表单的另一台服务器上。直到最近,它一直运作良好,但一些不相关的变化似乎把事情搞砸了。我想我知道可能发生了什么,但让我先解释一下问题

起初我认为这是一个问题与去润滑!埃雷吉命令。所以,我把它们都改成了!赛前,但这显然不是唯一的问题

表单似乎正常工作,没有报告错误,成功页面正在解析,但从未发送过电子邮件(是的,我检查了垃圾邮件文件夹)。大约在问题开始的同一时间,我将原始php邮件脚本所在的网站和域移动到另一台服务器上(我无法在该服务器上托管php文件),但将php邮件脚本保留在旧服务器上。当然,没有指向该web服务器的域,外部引用就停止了工作。因此,我只是将文件放到旧服务器上的一个子域中,并相应地重新引用表单。它现在连接良好,正如我所说,将脚本解析到成功页面

此服务器的电子邮件宿主从未更改。所以,这不应该是一个问题,但我认为这个问题与域名变更有关。有什么想法吗?脚本和表单地址如下:

    <?php

ini_set("display_errors", "1");
error_reporting(E_ALL);

if(isset($_POST['email'])) {
//  set the EMAIL TO options
    $email_to = "jr@creativeheat.co.uk";
    $email_bcc = "jr@creativeheat.co.uk";
    $email_subject = "Website booking inquiry";
//  grab referal info from POST
    $path = explode('/', $_SERVER['HTTP_REFERER']);
    $referer = $path[2];
//  redirect to error page
    function died($error) {
    $path = explode('/', $_SERVER['HTTP_REFERER']);
    $referer = $path[2];
        header( 'Location: http://'.$referer.'/'.$error ) ;
        exit;
    }

//  grab the checkbox values and change them to strings
    if (isset($_POST['booking_0'])) { $book1 = $_POST['booking_0']; if( $book1 == 'Bedroom(s)') {   $book1 = "                  The Bedroom(s) \n"; }} else {$book1 = "\n";}
    if (isset($_POST['booking_1'])) { $book2 = $_POST['booking_1']; if( $book2 == 'Meeting Room') { $book2 = "                  The Meeting Room \n";}} else {$book2 = "\n";}
    if (isset($_POST['booking_2'])) { $book3 = $_POST['booking_2']; if( $book3 == 'Barn') { $book3 = "                  The Barn \n";   }} else {$book3 = "\n";}
    if (isset($_POST['booking_3'])) { $book4 = $_POST['booking_3']; if( $book4 == 'Campsite') { $book4 = "                  The Campsite \n";}} else {$book4 = "\n";}
//  clear the ERRORTYPE & ERROR_MESSAGE variables
    $errortype = "";
    $error_message = "";
//  then check for an all false in the checkbox group
    if (!isset($_POST['booking_0']) && !isset($_POST['booking_1']) && !isset($_POST['booking_2']) && !isset($_POST['booking_3'])) {
//  provided none of the checkboxes are ticked set the DIED function parameter to ERRORTYPE = BOOKINGERR
        $error_message = 'error';
        $errortype = 'bookingerr';
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
//  alternate bruteforce redirect to NO BOOKING TYPE SELECTED page
//      header( 'Location: http://'.$referer.'/booking/'.$errortype ) ;
    }

//  check everything else
//  reset the ERROR variables
    $errortype = "";
    $error_message = "";
//  check the ISSET state of the remaining required fields
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['number']) ||
        !isset($_POST['email']) ||
        !isset($_POST['adults']) ||
        !isset($_POST['children']) ||
        !isset($_POST['from_date']) ||
        !isset($_POST['to_date']) ||
        !isset($_POST['disabled']) ||
        !isset($_POST['parking']) ||
        !isset($_POST['general'])) {
//   redirect to GENERAL INVALIDATION page
        $error_message = 'error';
        $errortype = 'requirederror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }

//  set FIELD variables
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $telephone = $_POST['number']; // required
    $email_from = $_POST['email']; // required
    $adults = $_POST['adults']; // required
    $children = $_POST['children']; // required
    $fdate = $_POST['from_date']; // required
    $tdate = $_POST['to_date']; // required
    $disabled = $_POST['disabled']; // not required
    $parking = $_POST['parking']; // not required
    $comments = $_POST['general']; // not required


//  begin INVALID field character checks
    $email_exp = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i";
    $errortype = "";
    $error_message = "";
    if(!preg_match($email_exp,$email_from)) {
//  redirect to INVALID EMAIL page
    $error_message = 'error';
    $errortype = 'emailinvalid';
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
    $string_exp = "/^[a-z .'-]+$/i";
    $errortype = "";
    $error_message = "";
    if(!preg_match($string_exp,$first_name)) {
//  redirect to INVALID FIRSTNAME page
    $error_message = 'error';
    $errortype = 'fnameerror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
    $errortype = "";
    $error_message = "";
    if(!preg_match($string_exp,$last_name)) {
//  redirect to INVALID LASTNAME page
    $error_message = 'error';
    $errortype = 'lnameerror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
    $errortype = "";
    $error_message = "";
    if(strlen($comments) < 2 && strlen($comments) > 0) {
//  redirect to INVALID COMMENTS page
        $error_message = 'error';
        $errortype = 'commentserror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
    $string_exp = "/^[0-9 .-]+$/i";
    $errortype = "";
    $error_message = "";
    if(!preg_match($string_exp,$telephone)) {
//  redirect to INVALID TELEPHONE page
    $error_message = 'error';
    $errortype = 'telephoneerror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
//  failsafe
    if(strlen($error_message) > 0) {
        died($errortype) ;
    }


//  begin EMAIL MESSAGE creation
    $email_message = "Form details below.\n\n";
//  remove ILLEGAL data from submitted fields   
    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }
//  set EMAIL_MESSAGE variable from data gathered from form 
    $email_message .= "Name:             ".clean_string($first_name)." ".clean_string($last_name)."\n";
    $email_message .= "Contact number:   ".clean_string($telephone)."\n";
    $email_message .= "Email address:    ".clean_string($email_from)."\n\n";
    $email_message .= "Interested in availability of the following: \n";
    $email_message .= $book1.$book2.$book3.$book4."\n";
    $email_message .= "Date from:        ".clean_string($fdate)."\n";
    $email_message .= "Date to:          ".clean_string($tdate)."\n\n";
    $email_message .= "Number of...\n";
    $email_message .= "Adults:           ".clean_string($adults)."\n";
    $email_message .= "Children:         ".clean_string($children)."\n\n";
    $email_message .= "Disabled?         ".clean_string($disabled)."\n";
    $email_message .= "Parking?          ".clean_string($parking)."\n\n";
    $email_message .= "Additional Information: \n\n";
    $email_message .= clean_string($comments);
//  create EMAIL HEADERS
    $headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n".'Bcc: '.$email_bcc."\r\n".'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
//  redirect to SUCCESS page
    header( 'Location: http://'.$referer.'/success' ) ;
    exit;
}
?>

表格地址:

编辑:

安装PHPmailer后,代码如下所示:

<?php
// new
require_once('class.phpmailer.php');
//end new

ini_set("display_errors", "1");
error_reporting(E_ALL);

if(isset($_POST['email'])) {
//  set the EMAIL TO options
    $email_to = "jr@creativeheat.co.uk";
    $email_bcc = "jr@creativeheat.co.uk";
    $email_subject = "Website booking inquiry";
//  grab referal info from POST
    $path = explode('/', $_SERVER['HTTP_REFERER']);
    $referer = $path[2];
//  redirect to error page
    function died($error) {
    $path = explode('/', $_SERVER['HTTP_REFERER']);
    $referer = $path[2];
        header( 'Location: http://'.$referer.'/'.$error ) ;
        exit;
    }

//  grab the checkbox values and change them to strings
    if (isset($_POST['booking_0'])) { $book1 = $_POST['booking_0']; if( $book1 == 'Bedroom(s)') {   $book1 = "                  The Bedroom(s) \n"; }} else {$book1 = "\n";}
    if (isset($_POST['booking_1'])) { $book2 = $_POST['booking_1']; if( $book2 == 'Meeting Room') { $book2 = "                  The Meeting Room \n";}} else {$book2 = "\n";}
    if (isset($_POST['booking_2'])) { $book3 = $_POST['booking_2']; if( $book3 == 'Barn') { $book3 = "                  The Barn \n";   }} else {$book3 = "\n";}
    if (isset($_POST['booking_3'])) { $book4 = $_POST['booking_3']; if( $book4 == 'Campsite') { $book4 = "                  The Campsite \n";}} else {$book4 = "\n";}
//  clear the ERRORTYPE & ERROR_MESSAGE variables
    $errortype = "";
    $error_message = "";
//  then check for an all false in the checkbox group
    if (!isset($_POST['booking_0']) && !isset($_POST['booking_1']) && !isset($_POST['booking_2']) && !isset($_POST['booking_3'])) {
//  provided none of the checkboxes are ticked set the DIED function parameter to ERRORTYPE = BOOKINGERR
        $error_message = 'error';
        $errortype = 'bookingerr';
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
//  alternate bruteforce redirect to NO BOOKING TYPE SELECTED page
//      header( 'Location: http://'.$referer.'/booking/'.$errortype ) ;
    }

//  check everything else
//  reset the ERROR variables
    $errortype = "";
    $error_message = "";
//  check the ISSET state of the remaining required fields
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['number']) ||
        !isset($_POST['email']) ||
        !isset($_POST['adults']) ||
        !isset($_POST['children']) ||
        !isset($_POST['from_date']) ||
        !isset($_POST['to_date']) ||
        !isset($_POST['disabled']) ||
        !isset($_POST['parking']) ||
        !isset($_POST['general'])) {
//   redirect to GENERAL INVALIDATION page
        $error_message = 'error';
        $errortype = 'requirederror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }

//  set FIELD variables
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $telephone = $_POST['number']; // required
    $email_from = $_POST['email']; // required
    $adults = $_POST['adults']; // required
    $children = $_POST['children']; // required
    $fdate = $_POST['from_date']; // required
    $tdate = $_POST['to_date']; // required
    $disabled = $_POST['disabled']; // not required
    $parking = $_POST['parking']; // not required
    $comments = $_POST['general']; // not required


//  begin INVALID field character checks
    $email_exp = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i";
    $errortype = "";
    $error_message = "";
    if(!preg_match($email_exp,$email_from)) {
//  redirect to INVALID EMAIL page
    $error_message = 'error';
    $errortype = 'emailinvalid';
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
    $string_exp = "/^[a-z .'-]+$/i";
    $errortype = "";
    $error_message = "";
    if(!preg_match($string_exp,$first_name)) {
//  redirect to INVALID FIRSTNAME page
    $error_message = 'error';
    $errortype = 'fnameerror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
    $errortype = "";
    $error_message = "";
    if(!preg_match($string_exp,$last_name)) {
//  redirect to INVALID LASTNAME page
    $error_message = 'error';
    $errortype = 'lnameerror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
    $errortype = "";
    $error_message = "";
    if(strlen($comments) < 2 && strlen($comments) > 0) {
//  redirect to INVALID COMMENTS page
        $error_message = 'error';
        $errortype = 'commentserror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
    $string_exp = "/^[0-9 .-]+$/i";
    $errortype = "";
    $error_message = "";
    if(!preg_match($string_exp,$telephone)) {
//  redirect to INVALID TELEPHONE page
    $error_message = 'error';
    $errortype = 'telephoneerror' ;
        if(strlen($error_message) > 0) {
            died($errortype) ;
        }
    }
//  failsafe
    if(strlen($error_message) > 0) {
        died($errortype) ;
    }


//  begin EMAIL MESSAGE creation
    $email_message = "Form details below.\n\n";
//  remove ILLEGAL data from submitted fields   
    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }
//  set EMAIL_MESSAGE variable from data gathered from form 
    $email_message .= "Name:             ".clean_string($first_name)." ".clean_string($last_name)."\n";
    $email_message .= "Contact number:   ".clean_string($telephone)."\n";
    $email_message .= "Email address:    ".clean_string($email_from)."\n\n";
    $email_message .= "Interested in availability of the following: \n";
    $email_message .= $book1.$book2.$book3.$book4."\n";
    $email_message .= "Date from:        ".clean_string($fdate)."\n";
    $email_message .= "Date to:          ".clean_string($tdate)."\n\n";
    $email_message .= "Number of...\n";
    $email_message .= "Adults:           ".clean_string($adults)."\n";
    $email_message .= "Children:         ".clean_string($children)."\n\n";
    $email_message .= "Disabled?         ".clean_string($disabled)."\n";
    $email_message .= "Parking?          ".clean_string($parking)."\n\n";
    $email_message .= "Additional Information: \n\n";
    $email_message .= clean_string($comments);
//  create EMAIL HEADERS
    $headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n".'Bcc: '.$email_bcc."\r\n".'X-Mailer: PHP/' . phpversion();
//new
    new PHPMailer($email_to, $email_subject, $email_message, $headers);
//end new

// old  @mail($email_to, $email_subject, $email_message, $headers);  
//end old
//  redirect to SUCCESS page
    header( 'Location: http://'.$referer.'/success' ) ;
    exit;
}
?>


尝试在PHP参考中记录的$header上仅使用“\n”而不是“\r\n”

尝试PHPMailer API,它有很多选项,并且可以在一行中轻松发送电子邮件。。删除邮件前面的@开始使用@已尝试使用@删除来捕获错误代码,但没有。也将尝试使用php mailer,谢谢。如果没有收到php错误,是否尝试检查服务器邮件日志以查看是否被阻止?也许您需要使用SMTP身份验证?