某些PHP字段不会显示在来自表单的电子邮件中

时间:2018-12-07 19:33:02

标签: php forms

我正在使用模板,并且主题随附的表单中的预制字段正在发送到我的电子邮件中,但是,我添加了其他字段,但这些字段未发布。电话,位置,联系时间和日期标签都会显示,但这些字段的输入数据不会随表格一起发送。

<?php
// Your Email
$recipient = "myemail@email.com"; // PLEASE SET YOUR EMAIL ADDRESS
$recaptcha_secret_key = 'yourAPIkey'; // PLEASE SET YOUR GOOGLE RECAPTCHA API KEY. If you are not using, please leave it blank.

if(!empty($recaptcha_secret_key) && array_key_exists('recaptcha',$_POST) ) {
    returnAndExitAjaxResponse(
        constructAjaxResponseArray(
            FALSE,
            'RECAPTCHA_FAILED_CHECK',
            array('error_message'=> 'RECAPTCHA_FAILED_CHECK')
        )
    );
}

// Recaptcha check up
// Requires curl. If your server does not support curl, this script does not work.
if(isset($_POST["g-recaptcha-response"]) && $recaptcha_secret_key !== '') {
    $endpoint = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $recaptcha_secret_key . '&response=' . $_POST['g-recaptcha-response'] ;
    $curl = curl_init() ;
    curl_setopt( $curl , CURLOPT_URL , $endpoint ) ;
    curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false ) ;
    curl_setopt( $curl , CURLOPT_RETURNTRANSFER , true ) ;
    curl_setopt( $curl , CURLOPT_TIMEOUT , 5 ) ;
    $json = curl_exec( $curl ) ;
    curl_close( $curl ) ;

    if($json == FALSE) {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'RECAPTCHA_FAILED',
                array('error_message'=> 'RECAPTCHA_FAILED')
            )
        );
    }

    $json = json_decode($json);

    if($json->success === FALSE) {
        returnAndExitAjaxResponse(
            constructAjaxResponseArray(
                FALSE,
                'RECAPTCHA_FAIL_RESPONSE',
                array('error_message'=> 'Recaptcha response is not valid.')
            )
        );
    }
}

// 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_STRING);

$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);

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

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

// Subject
$subject = "New booking from contact form";

// Build Message
$email_content = "First Name: $fname\n";
$email_content .= "Last Name: $lname\n";
$email_content .= "Phone: $phone\n";
 $email_content .= "Date: $date\n"; 
$email_content .= "Email: $email\n\n";
$email_content .= "Location: $location\n";
$email_content .= "Preferred Contact Time: $contacttime\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;
}

这是我的表单部分:

<!-- Contact Form -->
            <section class="section-block replicable-content contact-2">
                <div class="row">


                    <div class="column width-8">
                        <div class="contact-form-container">
                            <form class="contact-form" action="php/send 
email.php" method="post" novalidate>
                                <div class="row">
                                    <div class="column width-6">
                                        <div class="field-wrapper">
                                            <input type="text" name="fname" 
class="form-fname form-element large" placeholder="First Name*" tabindex="1" 
required>
                                        </div>
                                    </div>
                                    <div class="column width-6">
                                        <div class="field-wrapper">
                                            <input type="text" name="lname" 
class="form-lname form-element large" placeholder="Last Name*" tabindex="2" 
required>
                                        </div>
                                    </div>
                                    <div class="column width-6">
                                        <div class="field-wrapper">
                                            <input type="email" name="email" 
class="form-email form-element large" placeholder="Email address*" 
tabindex="3" 
required>
                                        </div>
                                    </div>
                                    <div class="column width-6">
                                        <div class="field-wrapper">
                                            <input type="tel" name="phone" 
class="form-element large" placeholder="Phone*" tabindex="4">
                                        </div>
                                    </div>
                                    <div class="column width-6">
                                        <div class="field-wrapper">
                                            <input type="text" name="date" 
class="form-element large" placeholder="Test (SAT ACT GRE) & Test Date*" 
 tabindex="5">

                                        </div>
                                    </div>
                                    <div class="column width-6">
                                        <div class="field-wrapper">
                                        <input type="text" name="location" 
class=" form-element large" placeholder="City & State*" tabindex="6" >
                                        </div>
                                    </div>
                                     <div class="column width-6">
                                         <div class="field-wrapper">
                                        <input type="text" 
 name="contacttime" class=" form-element large" placeholder="Preferred 
Contact Date & Time*" tabindex="7" >
                                         </div>
                                    </div>

                                    <div class="column width-6">
                                        <input type="text" name="honeypot" 
class="form-honeypot form-element">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="column width-12">
                                        <div class="field-wrapper">
                                            <textarea name="message" 
class="form-message form-element large" placeholder="Message*" tabindex="7" 
required></textarea>
                                        </div>
                                    </div>
                                    <div class="column width-12">
                                        <input type="submit" value="Send 
Email" class="form-submit button medium bkg-greem color-white color-hover- 
white" >
                                    </div>
                                </div>
                            </form>
                            <div class="form-response"></div>
                        </div>
                    </div>
                </div>



            </section>
            <!--Contact Form End -->

很抱歉,如果格式不正确,这是我的第一篇文章。

0 个答案:

没有答案