做网站产品介绍,设计房子装修效果图软件,学做网站论坛vip号码,网站建设报价清单明细我一直在使用PHP mail()函数。如果邮件由于任何原因未发送#xff0c;我想回显错误消息。 我该怎么做#xff1f;就像是$this_mail mail(exampleexample.com, My Subject, $message);if($this_mail) echo sent!;else echo error_message;谢谢#xff01;当mail()返回false时…我一直在使用PHP mail()函数。如果邮件由于任何原因未发送我想回显错误消息。 我该怎么做就像是$this_mail mail(exampleexample.com, My Subject, $message);if($this_mail) echo sent!;else echo error_message;谢谢当mail()返回false时可以使用error_get_last()。$success mail(exampleexample.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 atx.x.x.x port 25, verify yourSMTP andsmtp_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/a34ecd9ef195c37c8354ab58f7bfcc9berror_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 yourservers mailing system accepted the message for delivery, and doesnot and can not in any way know whether or not you are providing validarguments. For example, the return value would be false if sendmailfailed to load (e.g. if it wasnt installed properly), but wouldreturn true if sendmail loaded properly but the recipient addressdoesnt exist.我确认这一点是因为在尝试在我的PHP脚本中使用mail()失败之后结果发现我的计算机上未安装sendmail但是php.ini变量sendmail_path为/usr/sbin/sendmail -t -i1-我从软件包管理器shell dnf install sendmail安装了sendmail2-我开始了它shell service sendmail start3-现在如果任何PHP mail()函数失败我会发现/var/mail/目录下记录的sendmail程序错误。每个用户1个文件例如此片段摘自我的/var/mail/root文件The original message was received at Sun, 29 Jul 2018 22:37:51 0200from 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。不是最终决定是否交付而是基本上域是否存在以及地址是否为有效格式的电子邮件地址。$eerror_get_last();if($e[message]!){// An error function}error_get_last(); -返回上一次发生的错误您应该在代码中添加一些解释以免将来对他人有所帮助。如何回答同意以前的评论。请修改您的答案以包含一些说明。纯代码的答案对教育未来的SO读者几乎没有作用。您的答案在质量不高的审核队列中。尝试这个。如果我对任何文件有任何错误那么我的电子邮件ID上会出现错误邮件。创建两个文件index.php和checkErrorEmail.php并将它们上传到您的服务器。然后使用浏览器加载index.php。的index.phpinclude(checkErrorEmail.php);include(dereporting.php);$temp;echo hi .$temp;?checkErrorEmail.php// Destinationsdefine(ADMIN_EMAIL,pradeep.callus7hotmail.com);//define(LOG_FILE,/my/home/errors.log);// Destination typesdefine(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){echoerrno.$errno.,errstr.$errstr.,errfile.$errfile.,errline.$errline;if($errno){error_log(Error: $errstrerror on line $errline in file $errfile, DEST_EMAIL, ADMIN_EMAIL);}/*switch ($errno) {case E_USER_ERROR:// Send an e-mail to the administratorerror_log(Error: $errstrFatal error on line $errline in file $errfile, DEST_EMAIL, ADMIN_EMAIL);// Write the error to our log file//error_log(Error: $errstrFatal 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: $errstrin $errfile on line $errline, DEST_LOGFILE, LOG_FILE);break;case E_USER_NOTICE:// Write the error to our log file// error_log(Notice: $errstrin $errfile on line $errline, DEST_LOGFILE, LOG_FILE);break;default:// Write the error to our log file//error_log(Unknown error [#$errno]: $errstrin $errfile on line $errline, DEST_LOGFILE, LOG_FILE);break;}*/// Dont execute PHPs internal error handlerreturn 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 MailerZend_Mail或phpmailer)一起使用。