PHP邮件表单不生成邮件

时间:2014-06-29 07:29:04

标签: php forms email

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

起初我认为这是一个问题,即被删除的!eregi命令。所以,我把它们全部更改为!preg_match,但这显然不是唯一的问题。

表单似乎有效,没有报告错误,成功页面正在解析,但是没有发送电子邮件(是的,我检查了我的垃圾邮件文件夹)。大约在同一时间问题开始的时候,我将托管原始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;
}
?>

表单地址:http://www.claverhammeetinghouse.org.uk/booking/

编辑:

安装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;
}
?>

1 个答案:

答案 0 :(得分:0)

尝试在$ header中仅使用“\ n”而不是“\ r \ n”,如PHP参考中所述。 http://php.net/manual/de/function.mail.php