phpmailer不返回true或false

时间:2016-06-02 11:49:19

标签: php

我有显示错误发生时的状态的文本。如果用户名或电话号码错误,文本将显示警告。但是当我成功发送邮件时,我收到了,没有任何反应。我认为$mail->Send()不会返回true或事件为false

您能告诉我以下代码有什么问题吗?

我的代码:

 $mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->CharSet = "UTF-8";
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "sender@gmail.com";
$mail->Password = "12345";   

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

//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userPhone"]) || !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_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Phone       = filter_var($_POST["userPhone"], 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' => 'Tempy!'));
    die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
    $output = json_encode(array('type'=>'error', 'text' => 'input valid email!'));
    die($output);
}
if(!is_numeric($user_Phone)) //check entered data is numbers
{
    $output = json_encode(array('type'=>'error', 'text' => 'only phone number required'));
    die($output);
}
if(strlen($user_Message)<20) //check emtpy message
{
    $output = json_encode(array('type'=>'error', 'text' => 'Too short'));
    die($output);
}

    // send mail
$mail->SetFrom( 'sender@gmail.com' );
$mail->AddReplyTo( $user_Email);  
$mail->Subject = 'You have new inquiry from '.$_POST["userName"]; //Subject        line for emails;
$mail->Body = $user_Message.' -SĐT:'. $user_Phone.' - Email: '.$user_Email ;
$mail->AddAddress('recive@gmail.com');

if(!$mail->Send())
{
    $output = json_encode(array('type'=>'error', 'text' => 'error'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'success'));
    die($output);
}

2 个答案:

答案 0 :(得分:0)

<?php标记后添加此行。这将提示错误。

ini_set('display_errors',1);

答案 1 :(得分:0)

如果您使用此https://github.com/Synchro/PHPMailer

看起来如果(!$ mail-&gt; Send())应该是if(!$ mail-&gt; send())

根据例子。