From b2f82b6fb78ffe0016bd3e84e2ddf1492f124024 Mon Sep 17 00:00:00 2001 From: zevav Date: Thu, 18 Oct 2018 14:30:53 -0400 Subject: [PATCH] added support in vendors.gmail for 'label' and 'raw' searches, added these to documentation. --- README.rst | 4 ++++ go.py | 8 ++++++++ imbox/imbox.py | 1 + imbox/parser.py | 14 +++++++++----- imbox/vendors/gmail.py | 7 ++++++- 5 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 go.py diff --git a/README.rst b/README.rst index 9a8359a..fb02970 100644 --- a/README.rst +++ b/README.rst @@ -82,6 +82,10 @@ Usage # Messages from a specific folder messages_in_folder_social = imbox.messages(folder='Social') + # Some of Gmail's IMAP Extensions are supported (label and raw): + all_messages_with_an_attachment_from_martin = imbox.messages(folder='all', raw='from:martin@amon.cx has:attachment') + all_messages_labeled_finance = imbox.messages(folder='all', label='finance') + for uid, message in all_inbox_messages: # Every message is an object with the following keys diff --git a/go.py b/go.py new file mode 100644 index 0000000..1a57729 --- /dev/null +++ b/go.py @@ -0,0 +1,8 @@ +# coding: utf-8 +from imbox import Imbox +import ssl +i = Imbox('imap.gmail.com', 'zev@avtranscription.com', + 'vokhvstoizcfxsej', ssl_context=ssl._create_unverified_context()) +ms = i.messages( + label='payables') +print(len(ms)) diff --git a/imbox/imbox.py b/imbox/imbox.py index 785875e..9b7a7ad 100644 --- a/imbox/imbox.py +++ b/imbox/imbox.py @@ -90,6 +90,7 @@ class Imbox: self.connection.select( messages_class.FOLDER_LOOKUP.get((folder.lower())) or folder) msg = " from folder '{}'".format(folder) + del kwargs['folder'] else: msg = " from inbox" diff --git a/imbox/parser.py b/imbox/parser.py index a7c64ac..0749e19 100644 --- a/imbox/parser.py +++ b/imbox/parser.py @@ -60,7 +60,8 @@ def get_mail_addresses(message, header_name): for index, (address_name, address_email) in enumerate(addresses): addresses[index] = {'name': decode_mail_header(address_name), 'email': address_email} - logger.debug("{} Mail address in message: <{}> {}".format(header_name.upper(), address_name, address_email)) + logger.debug("{} Mail address in message: <{}> {}".format( + header_name.upper(), address_name, address_email)) return addresses @@ -111,7 +112,8 @@ def parse_attachment(message_part): name, value = decode_param(param) if 'file' in name: - attachment['filename'] = value[1:-1] if value.startswith('"') else value + attachment['filename'] = value[1:- + 1] if value.startswith('"') else value if 'create-date' in name: attachment['create-date'] = value @@ -163,9 +165,11 @@ def parse_email(raw_email, policy=None): email_parse_kwargs = {} try: - email_message = email.message_from_string(raw_email, **email_parse_kwargs) + email_message = email.message_from_string( + raw_email, **email_parse_kwargs) except UnicodeEncodeError: - email_message = email.message_from_string(raw_email.encode('utf-8'), **email_parse_kwargs) + email_message = email.message_from_string( + raw_email.encode('utf-8'), **email_parse_kwargs) maintype = email_message.get_content_maintype() parsed_email = {'raw_email': raw_email} @@ -187,7 +191,7 @@ def parse_email(raw_email, policy=None): content = decode_content(part) is_inline = content_disposition is None \ - or content_disposition.startswith("inline") + or content_disposition.startswith("inline") if content_type == "text/plain" and is_inline: body['plain'].append(content) elif content_type == "text/html" and is_inline: diff --git a/imbox/vendors/gmail.py b/imbox/vendors/gmail.py index 08d676e..f6cf3c8 100644 --- a/imbox/vendors/gmail.py +++ b/imbox/vendors/gmail.py @@ -26,5 +26,10 @@ class GmailMessages(Messages): parser_policy, **kwargs): - self.IMAP_ATTRIBUTE_LOOKUP['subject'] = '(X-GM-RAW "{}")' + self.IMAP_ATTRIBUTE_LOOKUP = {**self.IMAP_ATTRIBUTE_LOOKUP, **{'subject': '(X-GM-RAW "{}")', + 'label': '(X-GM-LABELS "{}")', + 'raw': '(X-GM-RAW "{}")', + } + } + super().__init__(connection, parser_policy, **kwargs)