php 如何获取 mail() 函数的错误消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3186725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the error message for the mail() function?
提问by Rohan
I've been using the PHP mail()function.
我一直在使用 PHPmail()函数。
If the mail doesn't send for any reason, I'd like to echo the error message. How would I do that?
如果邮件由于任何原因没有发送,我想回显错误消息。我该怎么做?
Something like
就像是
$this_mail = mail('[email protected]', 'My Subject', $message);
if($this_mail) echo 'sent!';
else echo error_message;
Thanks!
谢谢!
回答by user2317245
You can use error_get_last()when mail()returns false.
您可以error_get_last()在mail()返回 false时使用。
$success = mail('[email protected]', 'My Subject', $message);
if (!$success) {
$errorMessage = error_get_last()['message'];
}
With print_r(error_get_last()), you get something like this:
使用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()
[file] => C:\www\X\X.php
[line] => 2
[type] => 2
[message] => mail(): 无法在 "xxxx" 端口 25 连接到邮件服务器,请验证 php.ini 中的 "SMTP" 和 "smtp_port" 设置或使用 ini_set()
[file] = > C:\www\X\X.php
[行] => 2
回答by Sergey Eremin
sending mail in php is not a one-step process. mail() returns true/false, but even if it returns true, it doesn't mean the message is going to be sent. all mail() does is add the message to the queue(using sendmail or whatever you set in php.ini)
在 php 中发送邮件不是一个一步的过程。mail() 返回 true/false,但即使它返回 true,也不意味着消息将被发送。mail() 所做的就是将消息添加到队列中(使用 sendmail 或您在 php.ini 中设置的任何内容)
there is no reliable way to check if the message has been sent in php. you will have to look through the mail server logs.
没有可靠的方法来检查消息是否已在 php 中发送。您将不得不查看邮件服务器日志。
回答by amccausl
You can use the PEAR mailer, which has the same interface, but returns a PEAR_Error when there is problems.
您可以使用PEAR mailer,它具有相同的接口,但在出现问题时返回 PEAR_Error 。
回答by Accountant ?
In my case, I couldn't get the error message in my PHP script no matter what I do (error_get_last(), or ini_set('display_errors',1);) don't show the error message
就我而言,无论我做什么,我都无法在我的 PHP 脚本中收到错误消息 ( error_get_last(), 或ini_set('display_errors',1);) 不显示错误消息
according to this post
根据这篇文章
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.
$mail 的返回值仅指您的服务器的邮件系统是否接受了要发送的邮件,并且不会也不可能以任何方式知道您是否提供了有效的参数。例如,如果 sendmail 加载失败(例如,如果它没有正确安装),则返回值为 false,但如果 sendmail 加载正确但收件人地址不存在,则返回 true。
I confirm this because after some failed attempts to use mail()in my PHP scripts, it turns that sendmailwas not installed on my machine, however the php.ini variable sendmail_pathwas /usr/sbin/sendmail -t -i
我确认了这一点,因为在尝试mail()在我的 PHP 脚本中使用失败后sendmail,我的机器上没有安装它,但是 php.ini 变量sendmail_path是/usr/sbin/sendmail -t -i
1- I installed sendmail from my package manager shell> dnf install sendmail
1- 我从我的包管理器安装了 sendmail shell> dnf install sendmail
2- I started it shell> service sendmail start
2-我开始了 shell> service sendmail start
3- Now if any PHP mail()function fails I find the errors of the sendmailprogram logged under /var/mail/directory. 1 file per user
3- 现在,如果任何 PHPmail()函数失败,我会sendmail在/var/mail/目录下找到记录的程序错误。每个用户 1 个文件
For example this snippet is taken from my /var/mail/rootfile
例如,这个片段取自我的 /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 -----
<[email protected]>
(reason: 550 Host unknown)
My system is linux Fedora 28 with apache2.4 and PHP 7.2
我的系统是带有 apache2.4 和 PHP 7.2 的 linux Fedora 28
回答by Joseph
There is no error message associated with the mail()function. There is only a trueor falsereturned on whether the email was accepted for delivery. Not whether it ultimately gets delivered, but basically whether the domain exists and the address is a validly formatted email address.
没有与该mail()功能相关的错误消息。只有true或false返回电子邮件是否被接受交付。不是它最终是否被交付,而是基本上域是否存在并且地址是否是有效格式的电子邮件地址。
回答by Joseph
$e=error_get_last();
if($e['message']!==''){
// An error function
}
error_get_last();- return the last error that occurred
error_get_last(); - 返回最后发生的错误
回答by Pradeep
Try this. If I got any error on any file then I got error mail on my email id. Create two files index.phpand checkErrorEmail.phpand uploaded them to your server. Then load index.phpwith your browser.
尝试这个。如果我在任何文件上遇到任何错误,那么我的电子邮件 ID 上就会收到错误邮件。创建两个文件index.php并将checkErrorEmail.php它们上传到您的服务器。然后index.php用浏览器加载。
Index.php
索引.php
<?php
include('checkErrorEmail.php');
include('dereporting.php');
$temp;
echo 'hi '.$temp;
?>
checkErrorEmail.php
检查错误电子邮件.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");
?>
回答by prodigitalson
As the others have said, there is no error tracking for send mail it return the boolean result of adding the mail to the outgoing queue. If you want to track true success failure try using SMTP with a mail library like Swift Mailer, Zend_Mail, or phpmailer.
正如其他人所说,发送邮件没有错误跟踪,它返回将邮件添加到传出队列的布尔结果。如果您想跟踪真正的成功失败,请尝试将 SMTP 与邮件库(如 Swift Mailer、Zend_Mail 或 phpmailer)结合使用。

