2010-07-06 37 views

回答

9

在php中發送郵件不是一個一步的過程。 mail()返回true/false,但即使它返回true,也不表示消息將被髮送。所有的郵件()做的是將消息添加到隊列(使用sendmail或任何你在php.ini中設置)

有沒有可靠的方法來檢查消息是否已在PHP中發送。您將不得不查看郵件服務器日誌。

+0

噢,好的。感謝你的回答! – Rohan 2010-07-06 14:00:03

0

沒有與mail()函數關聯的錯誤消息。郵件是否被接受發送,僅返回truefalse。不是它是否最終得到交付,而是基本上是否存在域,地址是有效格式化的電子郵件地址。

-2

正如其他人所說,發送郵件沒有錯誤跟蹤,它返回郵件添加到傳出隊列的布爾結果。如果你想跟蹤真正的成功失敗,請嘗試使用SMTP和郵件庫,如Swift Mailer,Zend_Mail或phpmailer。

2

可以使用PEAR mailer,它具有相同的接口,但在出現問題時會返回PEAR_Error。

-1

試試這個。如果我在任何文件上遇到任何錯誤,那麼我的電子郵件ID上會收到錯誤郵件。創建兩個文件index.phpcheckErrorEmail.php並將它們上載到您的服務器。然後用您的瀏覽器加載index.php

的index.php

<?php 
    include('checkErrorEmail.php'); 
    include('dereporting.php'); 
    $temp; 
    echo 'hi '.$temp; 
?> 

checkErrorEmail.php

<?php 
    // Destinations 
    define("ADMIN_EMAIL", "[email protected]"); 
    //define("LOG_FILE", "/my/home/errors.log"); 

    // Destination types 
    define("DEST_EMAIL", "1"); 
    //define("DEST_LOGFILE", "3"); 

    /* Examples */ 

    // Send an e-mail to the administrator 
    //error_log("Fix me!", DEST_EMAIL, ADMIN_EMAIL); 

    // Write the error to our log file 
    //error_log("Error", DEST_LOGFILE, LOG_FILE); 

    /** 
    * my_error_handler($errno, $errstr, $errfile, $errline) 
    * 
    * Author(s): thanosb, ddonahue 
    * Date: May 11, 2008 
    * 
    * custom error handler 
    * 
    * Parameters: 
    * $errno: Error level 
    * $errstr: Error message 
    * $errfile: File in which the error was raised 
    * $errline: Line at which the error occurred 
    */ 

    function my_error_handler($errno, $errstr, $errfile, $errline) 
    { 
    echo "<br><br><br><br>errno ".$errno.",<br>errstr ".$errstr.",<br>errfile ".$errfile.",<br>errline ".$errline; 
     if($errno) 
     { 
       error_log("Error: $errstr \n error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL); 
     } 
    /*switch ($errno) { 
     case E_USER_ERROR: 
     // Send an e-mail to the administrator 
     error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_EMAIL, ADMIN_EMAIL); 

     // Write the error to our log file 
     //error_log("Error: $errstr \n Fatal error on line $errline in file $errfile \n", DEST_LOGFILE, LOG_FILE); 
     break; 

     case E_USER_WARNING: 
     // Write the error to our log file 
     //error_log("Warning: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE); 
     break; 

     case E_USER_NOTICE: 
     // Write the error to our log file 
     // error_log("Notice: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE); 
     break; 

     default: 
     // Write the error to our log file 
     //error_log("Unknown error [#$errno]: $errstr \n in $errfile on line $errline \n", DEST_LOGFILE, LOG_FILE); 
     break; 
    }*/ 

    // Don't execute PHP's internal error handler 
    return TRUE; 
    } 


    // Use set_error_handler() to tell PHP to use our method 
    $old_error_handler = set_error_handler("my_error_handler"); 


?> 
+2

什麼是包括('dereporting.php');? – Jon 2016-02-23 14:41:58

72

您可以使用error_get_last()mail()返回false。

$success = mail('[email protected]', 'My Subject', $message); 
if (!$success) { 
    $errorMessage = error_get_last()['message']; 
} 

隨着print_r(error_get_last()),你得到的東西是這樣的:

[類型] => 2
[消息] =>電子郵件():無法連接在 「XXXX」 端口25的郵件服務器上,驗證 「SMTP」 和 「SMTP_PORT」 在php.ini設置或使用的ini_set()
[文件] => C:\ WWW \ X \ X.php
[行] => 2

+7

這是我聽說過的這個錯誤命令中的第一個,也是我一直在尋找的東西,不知道它是否太長。謝謝! – 2013-12-03 19:17:53

+0

我認爲這隻適用於使用SMTP(Windows?)的情況。 在Linux上,如果您使用「sendmail」,「mail()」函數僅返回該命令的退出狀態:https://github.com/php/php-src/blob/PHP-5.6.25/ext/standard /mail.c#L404 沒有可靠的方法來獲取錯誤消息afaik。我嘗試了這個腳本:https://gist.github.com/njam/a34ecd9ef195c37c8354ab58f7bfcc9b – njam 2016-08-29 11:59:02

0
$e=error_get_last(); 
if($e['message']!==''){ 
    // An error function 
} 

error_get_last(); - 返回發生的最後一個錯誤

+6

你應該添加一些你的代碼的解釋,它可以在未來幫助其他人。 [answer] – cosmoonot 2017-04-20 06:03:17

+2

同意以前的評論。請編輯您的答案以包含一些解釋。僅有代碼的答案對未來SO讀者的教育很少。您的回答是在低質量的審覈隊列中。 – mickmackusa 2017-04-20 07:37:42