我的PHP联系人向我发送电子邮件,但部分内容留空

时间:2017-03-18 15:39:19

标签: php email

我正在使用一个主题,这是随附的PHP。我正在测试这些电子邮件,我注意到的一件事是,无论我做什么,有几个部分都会不断弹出。这是我第一次使用php而且我不确定是否应该删除主题php并创建我自己的主题。具体而言,数字,地址,城市,州,日期,邮政编码是显示空白的文本。我收到了电子邮件,但这些部分一直是空白的。我的代码不正确吗?

 <php>

          <?php    

if ($_POST['fname']) {

    // Your Email
    $recipient = "thttkidd@yahoo.com"; // PLEASE SET YOUR EMAIL ADDRESS

    // Check $recipient
    if($recipient === '') {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'RECIPIENT_IS_NOT_SET',
                array('error_message'=> 'RECIPIENT email address is not set. Please configure the script.')
            )
        );
    }

    // Check for empty required field
    if(!isset($_POST["email"]) || !isset($_POST["fname"]) || !isset($_POST["message"])) {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'MISSING_REQUIRED_FIELDS',
                array('error_message'=> 'MISSING_REQUIRED_FIELDS should not be occurred.')
            )
        );
    }

    // Sanitize input
    $fname  = filter_var($_POST["fname"], FILTER_SANITIZE_STRING);
    $lname  = filter_var($_POST["lname"], FILTER_SANITIZE_EMAIL);
    $website = $_POST["website"];
    if (!preg_match("~^(?:f|ht)tps?://~i", $website)) $website = "http://" . $website;
    $website = filter_var($website, FILTER_VALIDATE_URL);
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $message = filter_var($_POST["message"], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
$number = filter_var($_POST["number"], FILTER_SANITIZE_STRING);
$adress = filter_var($_POST["Adress"], FILTER_SANITIZE_STRING);
$City = filter_var($_POST["City"], FILTER_SANITIZE_STRING);
$State = filter_var($_POST["State"], FILTER_SANITIZE_STRING);
$Date = filter_var($_POST["date"], FILTER_SANITIZE_STRING);
$Zcode = filter_var($_POST["ZCode"], FILTER_SANITIZE_STRING);
var_dump($_POST);

    // If non required fields are empty
    if ( empty($lname) ){
        $lname = "No last name entered.";
    }

    // Headers
    $headers = 'From: '.$fname.' <'.$email.'>' . "\r\n";
    $headers .= 'Reply-To: '.$email.'' . "\r\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();

    // Subject
    $subject = "New email from book now form";

    // Build Message
    $email_content = "First Name: $fname\n";
    $email_content .= "Last Name: $lname\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Number: $number\n";
    $email_content .= "Address: $adress\n\n";
    $email_content .= "City: $City\n\n";
    $email_content .= "State $State\n\n";
    $email_content .= "Date $Date\n\n";
    $email_content .= "Zcode $Zcode\n\n";
    $email_content .= "Message:\n$message\n\n\n";
    $email_content .= "CLIENT IP:\n".get_client_ip()."\n";
    $email_content .= "HOST IP:\n".$_SERVER['SERVER_ADDR']."\n";

    // Check if sent
    try {
        $sendmailResult = mail($recipient, $subject, $email_content, $headers);
        if( $sendmailResult === TRUE ) {
            returnAndExitAjaxResponse(
                constructAjaxResponseArray(
                    TRUE
                )
            );
        } else {
            returnAndExitAjaxResponse(
                constructAjaxResponseArray(
                    FALSE,
                    'ERROR_AT_PHPMAIL',
                    array('error_information'=> error_get_last() )
                )
            );
        }
    } catch (Exception $_e) {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                TRUE,
                'ERROR_AT_PHPMAIL',
                array('error_message'=> $_e->getMessage())
            )
        );
    }

}
/*
    Construct ajax response array
    Input: Result (bool), Message (optional), Data to be sent back in array
*/
function constructAjaxResponseArray ($_response, $_message = '', $_json = null) {
    $_responseArray = array();
    $_response = ( $_response === TRUE ) ? TRUE : FALSE;
    $_responseArray['response'] = $_response;
    if(isset($_message)) $_responseArray['message'] = $_message;
    if(isset($_json)) $_responseArray['json'] = $_json;

    return $_responseArray;
}
/*
    Returns in the Gframe ajax format.
    Input: data array processed by constructAjaxResponseArray ()
    Outputs as a html stream then exits.
*/
function returnAndExitAjaxResponse ($_ajaxResponse) {
    if(!$_ajaxResponse){
        $_ajaxResponse = array('response'=>false,'message'=>'Unknown error occurred.');
    }
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode($_ajaxResponse);
    die();
}


// Function to get the client IP address
function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if(isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if(isset($_SERVER['HTTP_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } else if(isset($_SERVER['REMOTE_ADDR'])) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }
    return $ipaddress;
}

?>
    < HTML >
                <!--Contact Form -->
        <section class="section-block replicable-content contact-2 no-padding-top">
                <div class="row">
                    <div class="column width-8 offset-2 center">
                        <h2 class="mb-30"><strong>Book Your Appointment Now</strong></h2>
                        <div class="contact-form-container">
                            <form class="contact-form" action="" method="post" novalidate>
                                <div class="row">
                                    <div class="column width-6">
                                        <input type="text" name="fname" class="form-fname form-element large" placeholder="First Name*" tabindex="1" required>
                                    </div>
                                    <div class="column width-6">
                                        <input type="text" name="lname" class="form-lname form-element large" placeholder="Last Name*" tabindex="2" required>
                                    </div>
                                    <div class="column width-6">
                                        <input type="email" name="email" class="form-email form-element large" placeholder="Email address*" tabindex="3" required>
                    </div>
                    <div class="column width-6">
    <input type="number" name="number" class="form-number form-element large" placeholder="Phone*" tabindex="4" required>
                                    </div>                                      
                                    <div class="column width-6">
                                        <input type="text" name="Adress" class="form-address form-element large" placeholder="Street Address*" tabindex="5" required>
                                    </div>                                        
                                    <div class="column width-6">
                                        <input type="text" name="City" class="form-city form-element large" placeholder="City*" tabindex="6" required>
                                    </div>                                        
                                    <div class="column width-6">
                                        <input type="text" name="State" class="form-state form-element large" placeholder="State*" tabindex="7" required>
                                    </div>                                       
                                    <div class="column width-6">
                                        <input type="text" name="ZCode" class="form-zcode form-element large" placeholder="Zip Code*" tabindex="8" required>
                                    </div>
                                    <div class="column width-6">
                                            <input type="date" name="date" class="form-date form-element large" placeholder="Date*" tabindex="9" >
                                        </div>  
                                    <div class="column width-6">
                                        <div class="form-select form-element large">
                                            <select name="options" class="form-aux" data-label="Options" tabindex="10" required>
                                                <option>Time Window</option>
                                                <option value="">10AM-12PM</option>
                                                <option value="">12PM - 2PM</option>
                                                <option value="">2PM - 4PM</option>
                                                <option value="">4PM - 6PM</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="column width-12">
                                            <input type="junk" name="junk" class="form-junk form-element large" placeholder="Where Is Your Junk Located Ex. (Attic, Backyard,Shed, Front Yard, Inside Home Etc.)*" tabindex="9" required>
                                        </div>
                                    <div class="column width-12">
                                        <div class="form-select form-element large">
                                            <select name="options" class="form-aux" data-label="Options" tabindex="5">
                                                <option selected="selected" value="" >How'd You Find Us</option>
                                                <option value="">From A Friend</option>
                                                <option value="">Google</option>
                                                <option value="">Bing</option>
                                                <option value="">Kudzo</option>
                                                <option value="">Yelp</option>
                                                <option value="">Yahoo</option>
                                                <option value="">Angie's List</option>
                                                <option value="">Other</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="column width-6">
                                        <input type="text" name="honeypot" class="form-honeypot form-element large">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="column width-12">
                                        <div class="field-wrapper">
                                            <textarea name="message" class="form-message form-element large" placeholder="Briefly Describe what you need removed*" tabindex="7" required></textarea>
                                        </div>
                                    </div>
                                    <div class="column width-12">
                                        <input type="submit" value="Book Now" class="form-submit button medium bkg-theme bkg-hover-theme color-white color-hover-white">
                                    </div>
                                </div>
                            </form>
                            <div class="form-response center"></div>
                        </div>
                    </div>
                </div>
        </section>
<!--Contact Form End -->

2 个答案:

答案 0 :(得分:1)

似乎变量没有从$ _POST获得任何值

因此,在// Sanitize input行的正下方添加这些行

$number = filter_var($_POST["number"], FILTER_SANITIZE_STRING);
$adress = filter_var($_POST["Adress"], FILTER_SANITIZE_STRING);
$City = filter_var($_POST["City"], FILTER_SANITIZE_STRING);
$State = filter_var($_POST["State"], FILTER_SANITIZE_STRING);
$Date = filter_var($_POST["date"], FILTER_SANITIZE_STRING);
$Zcode = filter_var($_POST["ZCode"], FILTER_SANITIZE_STRING);

答案 1 :(得分:0)

您是否尝试过与主题相关的支持?他们可能会得到你(和我们)需要的信息,它应该如何工作,需要什么环境(即PHP5.6与PHP7.1,哪些模块启用/禁用),它会善于了解你正在使用的环境。