[英]php mail error handling when no smtp server
I make a php page to send email. I will give this page to multiple users, but I have no control of the server they will use.我制作了一个 php 页面来发送 email。我会将此页面提供给多个用户,但我无法控制他们将使用的服务器。 And some of them should have no SMTP server.
而且其中一些应该没有SMTP服务器。 So I try to handle this error.
所以我尝试处理这个错误。
For example, on my own computer, if I deactivate my SMTP server, I receive a "PHP Warning: mail(): Failed to connect to mailserver at..." error message.例如,在我自己的计算机上,如果我停用我的 SMTP 服务器,我会收到“PHP 警告:mail():无法连接到...的邮件服务器”错误消息。
My code:我的代码:
try
{
$okmail=mail($to,$Subject,$texte,$headers);
}
catch (Exception $ex)
{
...
}
But this code doesn't throw an exception, it only write the error message (like an "echo(...)" statement).但是这段代码不会抛出异常,它只会写入错误消息(如“echo(...)”语句)。 I use this code in a xmlhttprequest page and use an xml type response.
我在 xmlhttprequest 页面中使用此代码并使用 xml 类型的响应。 But the original page receive the error message before the xml text: PHP Warning: mail(): Failed to connect to mailserver at... \n1Email not send
但原始页面在 xml 文本之前收到错误消息:PHP Warning: mail(): Failed to connect to mailserver at... \n1Email not send
How can I handle this "no SMTP" error?我该如何处理这个“无 SMTP”错误?
Thanks!谢谢!
If I understand the problem correctly, the following directive will hide the warnings.如果我正确理解问题,则以下指令将隐藏警告。
ini_set('display_errors', 0);
Instead of "try/catch" you can use the following approach.您可以使用以下方法代替“try/catch”。
set_error_handler("warningHandler", E_USER_WARNING);
// Trigger some warning here for testing purposes only (this is just an example).
trigger_error('This here is the message from some warning ...', E_USER_WARNING);
// If you need DNS Resource Records associated with a hostname.
// $result = dns_get_record($_SERVER['HTTP_HOST']);
// Restores the previous error handler function.
restore_error_handler();
// This is your custom warning handler.
function warningHandler($errno, $errstr) {
// Do what you want here ...
echo $errno, ' ::: ', $errstr;
}
Thanks for your answers, but nothing works:(感谢您的回答,但没有任何效果:(
I find a solution by cleaning the output buffer by using ob_start(null,0,PHP_OUTPUT_HANDLER_CLEANABLE) and ob_clean() just after the mail() function.我找到了一个解决方案,方法是在 mail() function 之后使用 ob_start(null,0,PHP_OUTPUT_HANDLER_CLEANABLE) 和 ob_clean() 清理 output 缓冲区。
I don't understand why the mail function writes an error into the output buffer不明白为什么mail function向output缓冲区写入错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.