سؤال

I have a python script using imaplib that connects to a gmail account and sorts emails based on '+' tags found in the email address. For example: emails sent to myaccount+root.foo.bar@gmail.com get moved to root\foo\bar.

My logic goes as follows: extract tags -> attempt to create folders -> copy message to folder.

Ocassionally an email will come in with the same tags, but different casing. myaccount+root.FOO.BAR@gmail.com, for example, and leads to this output:

#Attempting to creating folder 'root/FOO/BAR'
('NO', ['[ALREADYEXISTS] Folder name conflicts with existing folder name. (Failure)'])
#Copying message to folder 'root/FOO/BAR'
('NO', ['[TRYCREATE] No folder root/FOO/BAR (Failure)'])

So it fails to create the folder, because a folder with the same name already exists (just with different case), but the copy fails because the explicit folder doesn't exist.

Is there some clever way that I can figure out the correct case of the existing folder so I can move the message without issue?

Note: This isn't as easy as just forcing all tags to lowercase. A User connects to the account with an email client and ocassioanlly manually makes folders with whatever casing makes sense to them at the time.

هل كانت مفيدة؟

المحلول

So Google complains that the folder already exists, but then gives an error when you try to move something into it? Terrific.

IMAP has a "LIST" command to list available mailboxes (folders):

http://tools.ietf.org/html/rfc3501#section-6.3.8

How to access that depends on your IMAP client library. Here are a couple of examples.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top