捕获PHP mail()错误并显示合理的用户错误消息 [英] Catching PHP mail() errors and showing reasonable user error message

查看:64
本文介绍了捕获PHP mail()错误并显示合理的用户错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个相当简单的注册php脚本,该脚本使用PHP的内置mail()函数通过电子邮件向用户发送激活链接.

I'm writing a fairly simple register php script that uses PHP's built in mail() function to email the user an activation link.

问题是我可以捕获诸如电子邮件格式之类的常规错误,但是一旦它触发到服务器并说用户输入的电子邮件地址失败,我不知道如何捕获此错误并告诉用户发生了什么事.

The problem is that I can catch the normal errors such as email formatting but once it fires off to the server and say a user has put in an email address that fails, I don't know how to catch this error and tell the user whats happened.

例如,目前我得到这个:

For example at the moment I get this:

警告:mail()[function.mail]:SMTP服务器响应:554 :收件人地址被拒绝:中继访问 在第70行的 ** 中被拒绝

Warning: mail() [function.mail]: SMTP server response: 554 : Recipient address rejected: Relay access denied in ** on line 70

有什么想法可以解决这样的错误吗?我知道使用@符号可以抑制错误,但我想做更多的事情来解决问题.

Any ideas what I could do about errors like this? I'm aware of using the @ symbol to suppress the error but I kinda of want to do more than that and handle the issue.

推荐答案

使用布尔结果检测错误:

Use the boolean result to detect an error:

$success = @mail(...);

然后,您要找出导致该问题的内部错误,请使用:

Then you want to find out which internal error caused the problem, so use:

$error = error_get_last();
preg_match("/\d+/", $error["message"], $error);
switch ($error[0]) {
    case 554:
        ...
    default:
        ...

请注意,这仅适用于php 5.2及更高版本.

Note that this works with php 5.2 onward only.

无法使用PHP验证传递或查看运输错误邮件.为此,您将需要一个pop3轮询处理程序.

There is no way to verify delivery or see transport error mails with PHP. You would need a pop3 polling handler for that.

这篇关于捕获PHP mail()错误并显示合理的用户错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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