Webklex / php-imap

PHP-IMAP is a wrapper for common IMAP communication without the need to have the php-imap module installed / enabled. The protocol is completely integrated and therefore supports IMAP IDLE operation and the "new" oAuth authentication process as well.
https://www.php-imap.com
MIT License
314 stars 147 forks source link

Error fetching emails: explode(): Empty delimiter #177

Closed dannylifino closed 2 years ago

dannylifino commented 3 years ago

Hello,

I am trying to fetch emails using the following code:. However I get this error in the Structure.php at line 130.

$inboxMessages = $this->inboxFolder->messages()->all()->get();

Webklex\PHPIMAP\Exceptions\GetMessagesFailedException

explode(): Empty delimiter

at C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:360 356▕ return $this->populate($available_messages); 357▕ } 358▕ return MessageCollection::make([]); 359▕ } catch (Exception $e) { ➜ 360▕ throw new GetMessagesFailedException($e->getMessage(), 0, $e); 361▕ } 362▕ } 363▕ 364▕ /**

1 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Structure.php:130 ErrorException::("explode(): Empty delimiter")

2 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Structure.php:130 explode("", "--

Webklex commented 3 years ago

Hi @dannylifino , many thanks for your report. Which version are you using? If you are using v3.0.0-alpha, please make sure to update your config and add config/imap.php#L151.

It seems like the regex could not be found in Header.php#L176.

Best regards,

dannylifino commented 3 years ago

@Webklex

Thanks for your fast response once again.

This does not seem to be the problem, however I have debugged the issue and what I found is that when the, Header.php#177 returns an empty string or '\n' for example. This results the parameter being used in the explode() function, and therefore, a delimiter error.

I wrote some code for the getBoundary() which solves this issue. `$regex = isset($this->config["boundary"]) ? $this->config["boundary"] : "/boundary=(.?(?=;)|(.))/i"; $boundary = $this->find($regex);

    if ($boundary === null) {
        return null;
    }

    $boundary = $this->clearBoundaryString($boundary);

    if ($boundary === '') {
        return null;
    }

    return $boundary;`

However after this i get a 'no content found' error, which torws another getMessagesFailedException:

Webklex\PHPIMAP\Exceptions\GetMessagesFailedException

no content found

at C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:360 356▕ return $this->populate($available_messages); 357▕ } 358▕ return MessageCollection::make([]); 359▕ } catch (Exception $e) { ➜ 360▕ throw new GetMessagesFailedException($e->getMessage(), 0, $e); 361▕ } 362▕ } 363▕ 364▕ /**

1 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:793 Webklex\PHPIMAP\Exceptions\GetMessagesFailedException::("no content found")

2 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:274 Webklex\PHPIMAP\Query\Query::handleException()

Webklex commented 3 years ago

Hi @dannylifino , can you check if the message you are fetching has actually a body? If it does, can you share the message here?

Best regards,

dannylifino commented 3 years ago

@Webklex

C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Structure.php:130 ErrorException::("explode(): Empty delimiter")

2 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Structure.php:130 explode("", "-- Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

This is a multipart Abuse report in ARF format for a message originating from the IP address 2.56.59.217, sent on 13 Oct 2021 18:17:25 -0700

This report consists of three sections:

You are receiving this email because you are an abuse contact listed in one of the following:

If you believe this is incorrect, please update your record with the relevant authorative body

Content-Disposition: inline Content-Transfer-Encoding: 8bit Content-Type: message/feedback-report

Source-Ip...")

Webklex commented 3 years ago

Hi @dannylifino , as you can see in the explode warning - it's missing the boundary. The message itself seems to be a multipart message. I'm sure there is a boundary somewhere.

In order to reproduce this, I'll need the message itself.

The message header should contain something like this:

Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_61853cc4deb13_6b6dc7241634e";
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Precedence: list

What boundary regex do you have configured inside config/imap.php#L151 and which version are you using?

Best regards,

dannylifino commented 3 years ago

@Webklex

'boundary' => '/boundary=(.?(?=;)|(.))/i',

And I am using the 3.0.0-alpha version

Webklex commented 3 years ago

Hi @dannylifino , many thanks for the feedback. Please try to replace https://github.com/Webklex/php-imap/blob/7574029286292167d44d7f1ceccaa73ee849bc84/src/Header.php#L179-L181

with:

if ($boundary === null || $boundary === "") {
    return null;
}

Does this solve the problem?

Best regards,

dannylifino commented 3 years ago

@Webklex

That reproduces this problem:

Webklex\PHPIMAP\Exceptions\GetMessagesFailedException

no content found

at C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:360 356▕ return $this->populate($available_messages); 357▕ } 358▕ return MessageCollection::make([]); 359▕ } catch (Exception $e) { ➜ 360▕ throw new GetMessagesFailedException($e->getMessage(), 0, $e); 361▕ } 362▕ } 363▕ 364▕ /**

1 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:793 Webklex\PHPIMAP\Exceptions\GetMessagesFailedException::("no content found")

2 C:\xampp\htdocs\portal\vendor\webklex\php-imap\src\Query\Query.php:274 Webklex\PHPIMAP\Query\Query::handleException()

Webklex commented 3 years ago

Hi @dannylifino , can you share the entire message / email? Otherwise I'm poking around in the dark. If you don't want to share it here, you can also mail it to github at webklex.com :)

Best regards,

Webklex commented 2 years ago

Hi @dannylifino , I'm closing this issue for now. Please give the latest release a try. If the issue persists, feel free to reopen this issue.

Best rgerads,