捕获 PHP mail() 错误并显示合理的用户错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4913817/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:15:27  来源:igfitidea点击:

Catching PHP mail() errors and showing reasonable user error message

php

提问by Cliftwalker

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

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

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:

例如目前我得到这个:

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

警告:mail() [function.mail]:SMTP 服务器响应:554:收件人地址被拒绝:第 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.

任何想法我可以对这样的错误做些什么?我知道使用@ 符号来抑制错误,但我还想做更多的事情并处理这个问题。

回答by mario

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:
        ...

Note that this works with php 5.2 onward only.

请注意,这仅适用于 php 5.2 以上。

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

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

回答by Yotam

A trivial error which I suffered from, was simply a lack of 'sendmail' in my system. Eventually, I have installed exim4and configured it - and then php's mail(...) worked fine.

我遭受的一个小错误只是我的系统中缺少“发送邮件”。最终,我安装了exim4并对其进行了配置 - 然后 php 的 mail(...) 工作正常。

See also:

也可以看看:

回答by Kristoffer Sall-Storgaard

You want to catch as much as you can before you send off the mail, sanitize your inputs to make sure it really is what you expect it to be

您想在发送邮件之前尽可能多地捕捉,清理您的输入以确保它确实是您所期望的

You can use filter_varto check that the email address really is an address, check that integers falls within your allowed range, etc.

您可以使用filter_var来检查电子邮件地址是否真的是地址,检查整数是否在您允许的范围内等。

Set the mail header from address to a place where you can check for failed deliveries:

将邮件标头从地址设置为可以检查失败传递的位置:

$headers = 'From: [email protected]' . "\r\n";

You can also check that mail performed as expected:

您还可以检查邮件是否按预期执行:

if(mail($to,$subject,$message))

And a final thing, you probably don't want to display warningsin a live environment, you can turn those off, either through the ini file or using ini_set.

最后一件事,您可能不想在实时环境中显示警告,您可以通过 ini 文件或使用ini_set.

回答by Jan

Best would be to use a library like Swift Mailer that throws exceptions that can be easily handled.

最好是使用像 Swift Mailer 这样的库来抛出可以轻松处理的异常。