2010-07-06 16 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