无法通过ajax发送php电子邮件

时间:2014-06-28 05:24:12

标签: jquery ajax phpmailer

请检查邮件并回复我,我编码错误的地方。无法发送电子邮件,请检查php邮件配置是否有错误?总是去其他地方。

<?php

if($_POST)
{
    $to_Email = "xxx@gmail.com"; //Replace with recipient email address
    $subject = 'Thanks for choosing XXXXXX'; //Subject line for emails


    //check if its an ajax request, exit if not
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

        //exit script outputting json data
        $output = json_encode(
        array(
            'type'=>'error', 
            'text' => 'Request must come from Ajax'
        ));

        die($output);
    } 
    if(!isset($_POST["userName"]) || !isset($_POST["userPhone"]) || !isset($_POST["userEmail"]) || !isset($_POST["userCompany"]) || !isset($_POST["userFrom"]) || !isset($_POST["userDest"]) || !isset($_POST["userItems"]) || !isset($_POST["userMessage"]))
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
        die($output);
    }

    //Sanitize input data using PHP filter_var().
    $user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
    $user_Phone = filter_var($_POST["userPhone"], FILTER_SANITIZE_STRING);
    $user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
    $user_Company = filter_var($_POST["userCompany"], FILTER_SANITIZE_STRING);
    $user_From = filter_var($_POST["userFrom"], FILTER_SANITIZE_STRING);
    $user_Dest = filter_var($_POST["userDest"], FILTER_SANITIZE_STRING);
    $user_Items = filter_var($_POST["userItems"], FILTER_SANITIZE_STRING);  
    $user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

    //additional php validation
    if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
    {
        $output = json_encode(array('type'=>'error', 'text' =>'Name is too short or empty!'));
        die($output);
    }
    if(!is_numeric($user_Phone) || strlen($user_Phone)<10 || strlen($user_Phone)>10) //check entered data is numbers
    {
        $output = json_encode(array('type'=>'error', 'text' =>'Invalid Phone Number'));
        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);
    }
    if(strlen($user_From)<3) // If length is less than 3 it will throw an HTTP error.
    {
        $output = json_encode(array('type'=>'error', 'text' =>'Location Name is too short or empty!'));
        die($output);
    }
    if(strlen($user_Dest)<3) // If length is less than 4 it will throw an HTTP error.
    {
        $output = json_encode(array('type'=>'error', 'text' =>'Destination Name is too short or empty!'));
        die($output);
    }
    if(strlen($user_Items)<3) // If length is less than 4 it will throw an HTTP error.
    {
        $output = json_encode(array('type'=>'error', 'text' =>'Item Name is too short or empty!'));
        die($output);
    }
    if(strlen($user_Message)<5) //check emtpy message
    {
        $output = json_encode(array('type'=>'error', 'text' =>'Too short message! Please enter something.'));
        die($output);
    }

     //proceed with PHP email.
     $from = "xxx@xxx.com";
$headers = "From:" . $from;


$sentMail = mail("xxx@xxxx.com", $subject, $user_Message .'  -'.$user_Name, $user_Email);   


  if($sentMail)
    {
        $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_Name .' Thank you for your email'));
        die($output);
    }

    else{
        $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
        die($output);
    }
}
?>

有人请帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

$sentMail = mail("xxx@xxxx.com", $subject, $user_Message .' -'.$user_Name, $user_Email);

邮件功能的参数是

mail(to, subject, message, headers (optional))

$user_Email不是有效的标头。

答案 1 :(得分:0)

你在邮件的最后一个参数中添加了错误的标题...试试这个。

$sentMail = mail("xxx@xxxx.com", $subject, $user_Message .' -'.$user_Name, $headers);