Can't silence imap_open error notices in PHP

2020-06-16 09:39发布

问题:

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.



标签: php imap-open