如何获取mail()函数的错误消息?

时间:2010-07-06 13:45:45

标签: php email

我一直在使用PHP mail()函数。

如果邮件因任何原因没有发送,我想回复一下错误信息。我该怎么做?

这样的东西
$this_mail = mail('example@example.com', 'My Subject', $message);

if($this_mail) echo 'sent!';
else echo error_message;

谢谢!

8 个答案:

答案 0 :(得分:104)

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

$success = mail('example@example.com', 'My Subject', $message);
if (!$success) {
    $errorMessage = error_get_last()['message'];
}

使用print_r(error_get_last()),您可以得到以下内容:

  

[type] => 2
  [message] => mail():无法连接到“x.x.x.x”端口25的邮件服务器,验证php.ini中的“SMTP”和“smtp_port”设置或使用ini_set()
  [file] => C:\ WWW \ X \ X.php
  [line] => 2

答案 1 :(得分:13)

在php中发送邮件并非一步到位。 mail()返回true / false,但即使返回true,也不意味着将要发送消息。所有mail()都将消息添加到队列中(使用sendmail或您在php.ini中设置的任何内容)

没有可靠的方法来检查消息是否已在php中发送。你必须查看邮件服务器日志。

答案 2 :(得分:3)

您可以使用具有相同接口的PEAR mailer,但在出现问题时返回PEAR_Error。

答案 3 :(得分:2)

就我而言,无论我做什么(error_get_last()ini_set('display_errors',1);)都不显示错误消息,我都无法在PHP脚本中收到错误消息

根据this post

  

$ mail的返回值仅表示您是否   服务器的邮件系统接受了邮件以进行传递,并且   不,也无法以任何方式知道您是否提供有效的   论点。例如,如果sendmail的返回值将为false   无法加载(例如,如果未正确安装),但会   如果sendmail已正确加载但收件人地址返回true   不存在。

我确认这一点,是因为在我的PHP脚本中尝试使用mail()失败后,发现sendmail没有安装在我的机器上,但是php.ini变量sendmail_path被安装了。 /usr/sbin/sendmail -t -i

1-我从软件包管理器shell> dnf install sendmail安装了sendmail

2-我开始了shell> service sendmail start

3-现在,如果任何PHP mail()函数失败,我将发现sendmail目录下记录的/var/mail/程序的错误。每个用户1个文件

例如,此摘录摘自我的/var/mail/root文件

The original message was received at Sun, 29 Jul 2018 22:37:51 +0200
from localhost [127.0.0.1]
   ----- The following addresses had permanent fatal errors -----
<no-one@errorerrorerrorerror51248562221e542.com>
    (reason: 550 Host unknown)

我的系统是带有apache2.4和PHP 7.2的linux Fedora 28

答案 4 :(得分:1)

没有与mail()功能关联的错误消息。对于是否接受电子邮件,只返回truefalse。不是它最终是否交付,而是基本上是否存在域名,地址是有效格式的电子邮件地址。

答案 5 :(得分:0)

试试这个。如果我在任何文件上收到任何错误,那么我的电子邮件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", "pradeep.callus7@hotmail.com");
  //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");


?>

答案 6 :(得分:0)

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

error_get_last(); - 返回上次发生的错误

答案 7 :(得分:-1)

正如其他人所说,发送邮件没有错误跟踪它返回将邮件添加到传出队列的布尔结果。如果要跟踪真正的成功失败,请尝试使用SMTP和Swift Mailer,Zend_Mail或phpmailer等邮件库。