php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

More descriptive errors for connection setup failed

Open ricuss opened this issue 3 years ago • 6 comments

Hi @Webklex

We are currently running version 3.2 in production.

After we updated to 3.2, we noticed that the connection error messages became a generic 'Connection setup failed' message when there was an issue connecting to their imap server.

Before 3.2, the error messages were very descriptive and gave us a lot of useful information on why the connection failed, as sometimes it's not just wrong credentials.

I haven't been able to find a way to get the reason for the failure since the upgrade, is this possibly related to https://github.com/Webklex/php-imap/issues/270 ?

Do you know of a way to get the failure reason as apposed to just the 'Connection setup failed' message?

Thanks :)

ricuss avatar Sep 14 '22 03:09 ricuss

Hi @ricuss , do you have the same problem with the latest release 4.0.2?

Which error message are you expecting? I assume you are talking about the exception thrown within the ImapProtocol::connect() method? https://github.com/Webklex/php-imap/blob/master/src/Connection/Protocols/ImapProtocol.php#L61-L84

The exception thrown in https://github.com/Webklex/php-imap/blob/master/src/Connection/Protocols/ImapProtocol.php#L82 does contain the original error - at least in the latest release and should look similar in v 3.2. I don't remember changing it :)

Best regards & happy coding,

Webklex avatar Sep 14 '22 06:09 Webklex

Hi @Webklex

Edit: The below is for a mailbox where I just changed the password to be incorrect, I would expect an error message along the lines of 'Invalid Credentials .....'

The error is from the authenticate function in the Client class, see below:

image

We hit the AuthFailedException and then throw the ConnectionFailedException where I can access the AuthFailedException with ->getPrevious() but there is no message on the AuthFailedException, and the message on the ConnectionFailedException is the generic 'Connection setup failed`.

Previously we also had the connection wrapped in a try catch, and calling ->getMessage() on it would return the imap error string we got from the server. The message itself ranged in content, depending on what the server sent back, but it helped a lot in identifying why a connection could not be established.

I checked it out on 4.0.2 quickly, and get the same, see below.

image

Any idea why I might not be getting the reason for the failure?

Edit2: For reference. I am using the ClientManager to establish a connection, as credentials are variable and provided by the user. I instantiate the ClientManager with the correct information and then call ->connect, which is where the error is being thrown with the 'Connection Setup Failed' message

ricuss avatar Sep 14 '22 06:09 ricuss

Ah so we are talking about this method: https://github.com/Webklex/php-imap/blob/master/src/Client.php#L381-L393

It changed with this commit almost two years ago: https://github.com/Webklex/php-imap/commit/b4dc869147e5ecf840dd7c6c6a04776709b7ffca

I don't see where you are missing the exception. It looks like everything is there. In your case the authentication simply failed. Looking at https://github.com/Webklex/php-imap/blob/master/src/Connection/Protocols/ImapProtocol.php#L415-L441 it looks like the case you've described gets handled within it: https://github.com/Webklex/php-imap/blob/master/src/Connection/Protocols/ImapProtocol.php#L429-L435

Which errors besides bad credentials were you expecting to receive?

Webklex avatar Sep 14 '22 07:09 Webklex

When I did the upgrade I moved from the laravel one to the core php-imap, I wonder if that is maybe related.

An example of the type of error messages we used to get:

imap_open(): Couldn't open stream {imap2.redacted.co.za:143/imap/novalidate-cert}. Kerberos error: No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_1001) (try running kinit) for [xxx.xx.xx.xxx]; Retrying PLAIN authentication after AUTHENTICATE failed.; Can not authenticate to IMAP server: [CLOSED] IMAP connection broken (server response) 
map_open(): Couldn't open stream {outlook.office365.com:993/imap/novalidate-cert/ssl}. Retrying PLAIN authentication after AUTHENTICATE failed.; Retrying PLAIN authentication after AUTHENTICATE failed.; Can not authenticate to IMAP server: AUTHENTICATE failed.

I would like to get access to those messages again but haven't been able to.

As a condensed example to be able to reproduce on your side you can try the following:

 $cm = new ClientManager(config('imap'));

      $data = [
          'username'      => "[email protected]",
          'password'      => "invalid",
          'host'          => 'imap.gmail.com',
          'port'          => 993,
          'encryption'    => 'ssl',
          'protocol'      => 'imap',
          'validate_cert' => false,
      ];

      $oClient = $cm->make($data);

      try{
          $oClient->connect();
      }
      catch(\Exception $e){
         dump($e);
      }

I'm trying to get an error message similar to the ones above in the catch, which I'm not able to.

Which errors besides bad credentials were you expecting to receive?

We would sometimes get timeouts or other information which helped pinpoint why a connection could be refused. Sometimes the server is being blocked or there are other issues even if the credentials are correct.

On a side note, with 3.2 we get a lot of false positives for the AuthFailedException, where it would be thrown, but if you try again it will work. The error messages were also used as a secondary check to ensure the error is actually a credentials error. We have dealt with the false positives on our side by just retrying mailboxes again 5 minutes later which is a workaround but seems to be working. Not ideal but one thing at a time :) For right now getting access to those messages is what we are more concerned about.

ricuss avatar Sep 14 '22 07:09 ricuss

Thanks for the detailed description and your examples. I understand it now :) Unfortunately I don't have a solution or "easy fix" right now. However we are currently discussing the protocol response handling and plan to provide access to the send & received commands. It will certainly take some time, but should come with v5.x or v6.x.

I added a "feature request" label, to remind me to let you know if it's implemented.

Best regards & happy coding,

Webklex avatar Sep 14 '22 08:09 Webklex

Thanks for the time looking into this @Webklex , really appreciate it. I still need to update to v4 (got released just as I updated to v3 haha)

Have a good one

ricuss avatar Sep 14 '22 08:09 ricuss