Can't silence imap_open error notices in PHP

落花浮王杯 提交于 2019-12-09 11:22:32

问题


I am using PHP 5.3.5 and I am using

$this->marubox=@imap_open($this->server,$this->username,$this->password);

The @ sign should silence error reporting but it doesnt and I am sure that the error occurs on this line. I want my application to recognize the problem itself and react and get no NOTICE errors and I can't turn off error reporting for whole PHP because of my company development policy.

Without @ i am getting:

imap_open() [function.imap-open]: Couldn't open stream {pop3.seznam.cz:110/pop3}INBOX With it i get: Notice Unknown: Authentication failed (Authentication failed) (errflg=1)

If the login information is ok it opens the connection and no errors occur.

I always get NOTICE error when imap_open doesnt manage to connect and it's messing up with my JSON results. How to silence it please?


回答1:


I added

$this->marubox=@imap_open($this->server,$this->username,$this->password);
imap_errors();
imap_alerts();

and imap_errors(); and imap_alerts(); do the magic :)




回答2:


Two possibilities come to mind:

  1. You could set error_reporting in your php.ini, ini_set or .htaccess or similar so that the NOTICE is suppressed, but since you wan't your application to handle the error, this is probably not, what you need

  2. Implement your own error handling. This is not so difficult to do. You define a function for error hadnling and then tell PHP to use it instead of it's own default handler.

    //define

    function myHandler($errno, $errstr) {}

    //somewhere towards the beginning of your processing script

    set_error_handler("myHandler");

See set_error_handler for more. Also note that from the moment you register the handler, you're solely responsible. You can suppress or throw any errors you need/want.



来源:https://stackoverflow.com/questions/5422405/cant-silence-imap-open-error-notices-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!