Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gmail Draft message error: decodeMimeStr() Can not decode an empty string #314

Closed
nssnl opened this issue May 8, 2019 · 4 comments
Closed
Labels
enhancement This will enhance this library. need help Your support is required to solve this issue. needs investigation This will be tested / debugged or checked out.

Comments

@nssnl
Copy link

nssnl commented May 8, 2019

When looping (foreach) through all the returned id's of searchMailbox('ALL'), the loop breaks when getMailHeader tries to pull a draft gmail message.

Error: decodeMimeStr() Can not decode an empty string!

Makes sense, but how can I ignore draft gmail messages?

@Sebbo94BY Sebbo94BY added the needs investigation This will be tested / debugged or checked out. label May 8, 2019
@Sebbo94BY
Copy link
Collaborator

Sebbo94BY commented May 8, 2019

First of all: I've added support for parsing drafts with the above referenced commit. This change will be included in the next release.

With this change, you will be able to check each single email, if it's is a draft or not:

$mail = $mailbox->getMail($mailsIds[0]);

if($mail->isDraft) {
        echo "Mail is a draft!\n";
} else {
        echo "Mail is NOT a draft!\n";
}

Regarding your question, how you can ignore draft messages:
It seems like as there was once a search criteria to search for drafts only, but this isn't supported anymore.

In your code, you define your IMAP server and the optional mailbox folder like this:

$mailbox = new PhpImap\Mailbox(
	'{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
	'some@gmail.com', // Username for the before configured mailbox
	'*********', // Password for the before configured username
	__DIR__, // Directory, where attachments will be saved (optional)
	'UTF-8' // Server encoding (optional)
);

So in this case, you'll open the mailbox folder INBOX. This folder is by the way the default folder, when you do not specify any.

searchMailbox() would now only search for emails in this folder. Since drafts are usually stored in the DRAFTS folder, you should never see any draft mail id's in this example.

If you're looping through all mailbox folders, you may want to exclude emails from the drafts folder by checking the folder name.

How does your code look like, that you get drafts as well?

@Sebbo94BY Sebbo94BY added enhancement This will enhance this library. need help Your support is required to solve this issue. labels May 8, 2019
@nssnl
Copy link
Author

nssnl commented May 9, 2019

Thank you for your extensive response. The commit would solve the issue with the draft e-mails, thank you! However, there appears to be a different but -related- issue as well.

The code I'm using for now is in line with example #4 of the Wiki:

$hoststring = '{imap.gmail.com:993/imap/ssl/novalidate-cert}[Gmail]/All Mail';
$username   = [username]
$password   = [password];
$attachdir  = NULL;
$mailbox = new PhpImap\Mailbox($hoststring, $username, $password, $attachdir);
$mailsIds = $mailbox->searchMailbox('ALL');
if(!$mailsIds) exit('Mailbox is empty');
foreach($mailsIds as $num) {
	echo $num;
	$head = $mailbox->getMailHeader($num);
}

Initially, I tested with the mailbox [Gmail]/All Mail and found out that it was the draft email that caused the error.

After that, I tested the same code with mailbox INBOX, and the script gave the same error. However, in this case I did not check which e-mail caused the error as I assumed it would be a draft e-mail (linked to an e-mail in the inbox). Based on your message I understand draft e-mail would not appear in the Inbox (even if linked to an inbox message).

Therefore I did some research by isolating e-mails in a separate folder (gmail label). I this case, it is indeed not an draft email that causes the error, but an e-mail with no subject and only an image in the body.

decodeMimeStr() Can not decode an empty string!.

The email is printed below. I redacted unrelevant info, but did not remove any lines. Also the Subject is empty.

Delivered-To: xxx@xxx.xx
Received: [redacted]
X-Received: [redacted]
Return-Path: <xxxx@xxxx.xx>
Received: [redacted]
Received-SPF: [redacted]
Authentication-Results: [redacted]
Received: [redacted]
DKIM-Signature: [redacted]
X-Google-DKIM-Signature: [redacted]
X-Gm-Message-State: [redacted]
X-Received: [redacted]
Return-Path: <xxxx@xxxx.xxx>
Received: [redacted]
From: "xxxxxx" <xxxx@xxxx.xx>
Content-Type: image/png; x-mac-hide-extension=yes; x-unix-mode=0644; name="Screen Shot 2015-09-04 at 14.51.43.png"
Content-Transfer-Encoding: base64
Subject: 
Message-Id: [redacted]
Date: Fri, 4 Sep 2015 14:52:12 +0200
To: xxxx <xxx@xxxx.xx>
Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2104\))
Content-Disposition: inline; filename="Screen Shot 2015-09-04 at 14.51.43.png"
X-Mailer: Apple Mail (2.2104)

iVBORw0KGgoAAAANSUhEUgAAARAAAAFkCAYAAADouWTCAAAKoWlDQ1BJQ0MgUHJvZmlsZQAASImV

Message continues with image code. After image code there is nothing

I'm happy to share the original e-mail in a private message if that helps.

@nssnl
Copy link
Author

nssnl commented May 9, 2019

I just tested your new commit by manually updating the files, and I don't have the above mentioned errors anymore: so the commit solves the issue with draft emails, but also the other example in my email!

Thanks for your help!

@Sebbo94BY
Copy link
Collaborator

Great. Thanks for testing! :)

Yeah, I've just made sure, that decodeMimeStr() gets a string. Providing nothing doesn't make sense. ;)

As all issues mentioned in this issue have been solved, I'll close this issue. Feel free to add comments, if there is still anything else open. :)

Sebbo94BY added a commit that referenced this issue May 9, 2019
- Issue #313: Fixed skipping of args, when they are identical to their defaults
- Issue #314: Added support for parsing drafts
- Issue #277: Added PHPUnit tests to test functionality of decoding MIME strings
- Issue #253: Added charset property to IncomingMailAttachment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This will enhance this library. need help Your support is required to solve this issue. needs investigation This will be tested / debugged or checked out.
Projects
None yet
Development

No branches or pull requests

2 participants