Skip to content

Body is NULL when reading from Gmail folders - working solution/code hack #313

Open
@pavel-j

Description

@pavel-j

When reading messages from Gmail Sent folder, all messages are read with empty body (getHTMLBody/getTextBody methods return NULL or sometimes empty string, getRawBody return empty string).

$cm = new ClientManager([
	'options' => [
		'fetch' => \Webklex\PHPIMAP\IMAP::FT_PEEK,
		'sequence' => \Webklex\PHPIMAP\IMAP::ST_UID,
		'fetch_body' => true,
		'fetch_flags' => false,
		'soft_fail' => true,
		'rfc822' => true, // tried false tooo, no change
		'debug' => true,
		'uid_cache' => false,
		'fetch_order' => 'asc',
		'common_folders' => [
			"root" => "INBOX"
		],
		'decoder' => [
			'message' => 'utf8', // mimeheader
			'attachment' => 'utf8' // mimeheader
		],
	]
]);
$client = $cm->make([..gmail..]);
$client->connect();
$folder = $client->getFolder('test'); // Sent or any other non-inbox folder
$messages = $folder->messages()->all()->get();
foreach($messages as $message) {
	// all null or empty string, for all messages. besides of body, other mail attributes are OK
	var_dump($message->getHTMLBody(),$message->getTextBody(),$message->getRawBody());
}

DEBUG output (compressed/skipped actual messages)

  • after headers of all messages are successfully fetched, body is fetched empty
  • ends with TAG8 NO System Error (Failure)
OK Gimap ready for requests from 164.215.118.74 dn12mb12278152wmb
TAG1 LOGIN "[email protected]" "xxxxx"
<< * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS LITERAL- SPECIAL-USE APPENDLIMIT=35651584
TAG1 OK [email protected] authenticated (Success)
>> TAG2 LIST "" "*"
<< * LIST (\HasNoChildren) "/" "Error maily"
<< * LIST (\HasNoChildren) "/" "INBOX"
<< * LIST (\HasChildren \Noselect) "/" "Odlo&AX4-eno"
<< * LIST (\HasNoChildren) "/" "Odlo&AX4-eno/n&ARs-kdy"
<< * LIST (\HasChildren \Noselect) "/" "[Gmail]"
<< * LIST (\HasNoChildren \Important) "/" "[Gmail]/D&AW8-le&AX4-it&AOk-"
<< * LIST (\Drafts \HasNoChildren) "/" "[Gmail]/Koncepty"
<< * LIST (\HasNoChildren \Trash) "/" "[Gmail]/Ko&AWE-"
<< * LIST (\HasNoChildren \Sent) "/" "[Gmail]/Odeslan&AOE- po&AWE-ta"
<< * LIST (\HasNoChildren) "/" "test"
<< TAG2 OK Success
>> TAG3 LIST "" "*"
<< * LIST (\HasNoChildren) "/" "Error maily"
<< * LIST (\HasNoChildren) "/" "INBOX"
<< * LIST (\HasChildren \Noselect) "/" "Odlo&AX4-eno"
<< * LIST (\HasNoChildren) "/" "Odlo&AX4-eno/n&ARs-kdy"
<< * LIST (\HasChildren \Noselect) "/" "[Gmail]"
<< * LIST (\HasNoChildren \Important) "/" "[Gmail]/D&AW8-le&AX4-it&AOk-"
<< * LIST (\Drafts \HasNoChildren) "/" "[Gmail]/Koncepty"
<< * LIST (\HasNoChildren \Trash) "/" "[Gmail]/Ko&AWE-"
<< * LIST (\HasNoChildren \Sent) "/" "[Gmail]/Odeslan&AOE- po&AWE-ta"
<< * LIST (\Flagged \HasNoChildren) "/" "[Gmail]/S hv&ARs-zdi&AQ0-kou"
<< * LIST (\HasNoChildren \Junk) "/" "[Gmail]/Spam"
<< * LIST (\All \HasNoChildren) "/" "[Gmail]/V&AWE-echny zpr&AOE-vy"
<< * LIST (\HasNoChildren) "/" "test"
<< TAG3 OK Success
>> TAG4 SELECT "test"
<< * FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
<< * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing \*)] Flags permitted.
<< * OK [UIDVALIDITY 59] UIDs valid.
<< * 50 EXISTS
<< * 0 RECENT
<< * OK [UIDNEXT 151] Predicted next UID.
<< * OK [HIGHESTMODSEQ 5191349]
<< TAG4 OK [READ-WRITE] test selected. (Success)
>> TAG5 UID SEARCH ALL
<< * SEARCH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<< TAG5 OK SEARCH completed (Success)
>> TAG6 UID FETCH 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85 (FLAGS)
<< * 1 FETCH (UID 1 FLAGS (\Seen))
.......... 50 messages - all ok
<< TAG6 OK Success
>> TAG7 UID FETCH 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85 (RFC822.HEADER)
<< * 1 FETCH (UID 1 RFC822.HEADER {636}
.......... 50 messages - all ok
<< TAG7 OK Success
>> TAG8 UID FETCH 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85 (RFC822.TEXT)
<< TAG8 NO System Error (Failure)
array(0) {
}
>> TAG9 UID FETCH 1:* (UID)
<< * 1 FETCH (UID 1)
...

Finally found solution (code hack) to overcome this problem
ImapProtocol.php

public function content($uids, string $rfc = "RFC822", $uid = IMAP::ST_UID): array {
        //$result = $this->fetch(["$rfc.TEXT"], $uids, null, $uid);  // original, empty body
        $result = $this->fetch(["BODY[]"], $uids, null, $uid); // this works!
        return is_array($result) ? $result : [];
}

This led me to my solution
https://forum.aspose.com/t/fetch-message-via-imap-use-body-rather-than-rfc822/251835

Though solution works for me, I cannot prepare 'clean' pull request respecting architecture and options

  • this should probably be used in case of rfc822=false (or by some other option), but in message parseBody(), "RFC822" is hardcoded

OS: Debian 11
PHP: 8.0
Version 4.1.0
Provider: Gmail

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions