php获取错误信息函数,关于php:如何获取mail()函数的错误消息?

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

如果邮件由于任何原因未发送,我想回显错误消息。 我该怎么做?

就像是

$this_mail = mail('example@example.com', 'My Subject', $message);

if($this_mail) echo 'sent!';

else echo error_message;

谢谢!

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

$success = mail('example@example.com', 'My Subject', $message);

if (!$success) {

$errorMessage = error_get_last()['message'];

}

使用print_r(error_get_last()),您将获得如下内容:

[type] => 2

[message] => mail(): Failed to connect to mailserver at"x.x.x.x" port 25, verify your"SMTP" and"smtp_port" setting in php.ini or use ini_set()

我认为这仅在使用SMTP(Windows?)时有效。在Linux上,如果您使用" sendmail",则" mail()"函数只会返回该命令的退出状态:github.com/php/php-src/blob/PHP-5.6.25/ext/standard/mail.c# L404没有可靠的方法来获取错误消息afaik。我尝试使用以下脚本:gist.github.com/njam/a34ecd9ef195c37c8354ab58f7bfcc9b

error_get_last()返回NULL !!但是mail函数返回true!

为什么它的答案如此流行,但为什么它没有引起人们的关注呢?我不知道人们会怎么完全想念它。

@ashleedawg-我什至不知道这怎么引起了这么多的投票。我从未见过-见过error_get_last()与phps本机mail()函数一起工作。实际上,我只是勉强设置了错误的邮件,然后再尝试一次以确保;我什么也没得到。

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

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

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

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

根据这篇文章

The return value from $mail refers only to whether or not your

server's mailing system accepted the message for delivery, and does

not and can not in any way know whether or not you are providing valid

arguments. For example, the return value would be false if sendmail

failed to load (e.g. if it wasn't installed properly), but would

return true if sendmail loaded properly but the recipient address

doesn't exist.

我确认这一点是因为在尝试在我的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()函数失败,我会发现/var/mail/目录下记录的sendmail程序错误。每个用户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 -----

(reason: 550 Host unknown)

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

没有与mail()函数关联的错误消息。关于是否接受电子邮件发送,仅返回true或false。不是最终决定是否交付,而是基本上域是否存在以及地址是否为有效格式的电子邮件地址。

$e=error_get_last();

if($e['message']!==''){

// An error function

}

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

您应该在代码中添加一些解释,以免将来对他人有所帮助。如何回答

同意以前的评论。请修改您的答案以包含一些说明。纯代码的答案对教育未来的SO读者几乎没有作用。您的答案在质量不高的审核队列中。

尝试这个。如果我对任何文件有任何错误,那么我的电子邮件ID上会出现错误邮件。创建两个文件index.php和checkErrorEmail.php,并将它们上传到您的服务器。然后使用浏览器加载index.php。

的index.php

include('checkErrorEmail.php');

include('dereporting.php');

$temp;

echo 'hi '.$temp;

?>

checkErrorEmail.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"errno".$errno.",errstr".$errstr.",errfile".$errfile.",errline".$errline;

if($errno)

{

error_log("Error: $errstr

error on line $errline in file $errfile

", DEST_EMAIL, ADMIN_EMAIL);

}

/*switch ($errno) {

case E_USER_ERROR:

// Send an e-mail to the administrator

error_log("Error: $errstr

Fatal error on line $errline in file $errfile

", DEST_EMAIL, ADMIN_EMAIL);

// Write the error to our log file

//error_log("Error: $errstr

Fatal error on line $errline in file $errfile

", DEST_LOGFILE, LOG_FILE);

break;

case E_USER_WARNING:

// Write the error to our log file

//error_log("Warning: $errstr

in $errfile on line $errline

", DEST_LOGFILE, LOG_FILE);

break;

case E_USER_NOTICE:

// Write the error to our log file

// error_log("Notice: $errstr

in $errfile on line $errline

", DEST_LOGFILE, LOG_FILE);

break;

default:

// Write the error to our log file

//error_log("Unknown error [#$errno]: $errstr

in $errfile on line $errline

", 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");

?>

什么是include(dereporting.php);?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值