簡體   English   中英

phpmailer不返回true或false

[英]phpmailer does not return true or false

我有顯示錯誤發生時狀態的文本。 如果用戶名或電話號碼錯誤,則文本將顯示警告。 但是當我成功發送郵件后,我什么也沒收到。 我認為$mail->Send()不會返回true或event 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);
}

<?php標記后添加此行。 這將提示錯誤。

ini_set('display_errors',1);

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

看起來if(!$ mail-> Send())應該是if(!$ mail-> send())

根據示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM