Php邮件脚本 - 不包括已发送电子邮件中的表单值

时间:2016-01-27 08:34:47

标签: php wordpress email

我有一个php邮件脚本,由以下内容触发。

var theForm = document.getElementById( 'mailinglist-form' );

new stepsForm( theForm, {
    onSubmit : function( form ) {
    var messageEl = $('.final-message');
        // hide form
        $('.simform-inner').addClass('hide' );
        //remove cursor
        $('#q2').blur();
        //get input field values data to be sent to server
        post_data = {
            'user_name'     : $('input[name=name]').val(), 
            'user_email'    : $('input[name=email]').val()
        };

        //Ajax post data to server

$.post('mail.php', post_data, function(response){  
        if(response.type == 'error'){

        //load json data from server and output message     
        messageEl.html('Sorry, please try again later.');
        $(messageEl).addClass( 'show' );
            }else{
        messageEl.html(response.text);
        $(messageEl).addClass( 'show' );
        }

        }, 'json');
    }

} );

这用于调用以下mail.php文件

<?php
if($_POST)
{
$to_email = "myemail@gmail.com"; //Recipient email
$subject = "Mailing list request";
$from = 'anotheremail@gmail.com';    
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    $output = json_encode(array( //create JSON data
        'type'=>'error', 
        'text' => 'Sorry Request must be Ajax POST'
    ));
    die($output); //exit script outputting json data
} 

//Sanitize input data using PHP filter_var().
$user_name      = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
$user_email     = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);

//additional php validation
if(strlen($user_name)<3){ // If length is less than 3 it will output JSON error.
    $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
    die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
    $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
    die($output);
}
//email body
$message_body = $message."".$user_name." wants to sign up to your mailing list. Details below:\r\n\nName : ".$user_name."\r\nEmail : ".$user_email."\r\n";

//proceed with PHP email.
$headers = 'From: '.$from.'' . "\r\n" .
'Reply-To: '.$from.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$send_mail = mail($to_email, $subject, $message_body, $headers);

if(!$send_mail)
{
    //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
    $output = json_encode(array('type'=>'error', 'text' => 'Sorry, please try again later.'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'All done. Thanks for signing up.'));
    die($output);
}
}
?>

现在这一切似乎都有效,它会将来自“anotheremail”的电子邮件“myemail”发送一封包含用户名和电子邮件地址的电子邮件。

真的很奇怪的是,有时候消息的其余部分没有用户名或电子邮件地址。

任何人都可以告诉我这里发生了什么,我已经遇到了这个障碍。

感谢并抱歉格式不正确的代码......:P

1 个答案:

答案 0 :(得分:1)

我很惊讶,这并不能解决问题。

$from = 'anotheremail@gmail.com'&lt; - 最后没有; ...本来以为php引擎会对此异常。