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

Correctly decode subject header in gmail client #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions gmail_client/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name = "gmail-client",
version = "0.0.7.7",
version = "0.0.8.1",
author = "Wilberto Morales",
author_email = "[email protected]",
description = ("A Pythonic interface for Google Mail. Based of https://github.com/charlierguo/gmai"),
Expand Down