diff --git a/gmail_client/message.py b/gmail_client/message.py index 85063cc..32e6e4c 100644 --- a/gmail_client/message.py +++ b/gmail_client/message.py @@ -48,7 +48,7 @@ def parse_labels(headers): def parse_subject(encoded_header): if encoded_header is not None: - dh = encoded_header.encode('UTF-8') + dh = decode_email_header(encoded_header) else: dh = '' @@ -174,6 +174,7 @@ def __init__(self, mailbox, uid): self.gmail = mailbox.gmail if mailbox else None self.message = None + self.raw = None self.headers = {} self.subject = None @@ -301,15 +302,25 @@ def _parse(self, raw_message): if re.search(r'X-GM-MSGID (\d+)', raw_headers): self.message_id = re.search(r'X-GM-MSGID (\d+)', raw_headers).groups(1)[0] - def fetch(self): return self.message if self.message else self.forced_fetch() + def fetch(self): + if self.message: + return self.message + else: + if self.raw: + self._parse(self.raw) + return self.message + else: + return self.forced_fetch(parse=True) @property def has_attachments(self): return len(self.attachments) > 0 - def forced_fetch(self): + def forced_fetch(self, parse=True): _, results = self.gmail.imap.uid('FETCH', self.uid, '(BODY.PEEK[] FLAGS X-GM-THRID X-GM-MSGID X-GM-LABELS)') - self._parse(results[0]) + self.raw = results[0] + if parse: + self._parse(self.raw) return self.message diff --git a/setup.py b/setup.py index 9c6fadd..4f3aee0 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name = "gmail-client", - version = "0.0.7.7", + version = "0.0.8.1", author = "Wilberto Morales", author_email = "wilbertomorales777@gmail.com", description = ("A Pythonic interface for Google Mail. Based of https://github.com/charlierguo/gmai"),