如何获取 mail() 函数的错误消息? [英] How can I get the error message for the mail() function?

查看:28
本文介绍了如何获取 mail() 函数的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been using the PHP mail() function.

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

If the mail doesn't send for any reason, I'd like to echo the error message. How would I do that?

类似的东西

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

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

谢谢!

推荐答案

如果你在 Windows 上使用 SMTP,你可以使用 error_get_last()mail() 返回 false.请记住,这不适用于 PHP 的原生 mail() 函数.

If you are on Windows using SMTP, you can use error_get_last() when mail() returns false. Keep in mind this does not work with PHP's native mail() function.

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

使用print_r(error_get_last()),你会得到这样的结果:

With print_r(error_get_last()), you get something like this:

[类型] =>2
[消息] =>mail(): 无法连接到位于x.x.x.x"的邮件服务器;端口 25,验证您的SMTP"和smtp_port"在 php.ini 中设置或使用 ini_set()
[文件] =>C:wwwXX.php
[行] =>2

[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:wwwXX.php
[line] => 2

这篇关于如何获取 mail() 函数的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆