Skip to content

Commit

Permalink
fixed work with charsets
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Amirov committed Jul 17, 2014
1 parent 13f9ca6 commit 8cf920e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions email2pb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
PUSH_TYPE = 'note'

TRACE_FILE = 'curl.trace'
DEFAULT_ENCODING = 'utf-8'

parser = argparse.ArgumentParser(description='Send PushBullet PUSH based on email message')
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin,
Expand All @@ -34,13 +35,14 @@
subject = subject_coded.decode(charset)
else:
subject = subject_raw

body_text = ''
for part in msg.walk():
if part.get_content_type() == 'text/plain':
body_part = part.get_payload()
if part.get('Content-Transfer-Encoding', 'base64') == 'base64':
body_part = base64.decodestring(body_part)

body_part = body_part.decode(part.get_content_charset())
if body_text:
body_text = '%s\n%s' % (body_text, body_part)
else:
Expand All @@ -55,7 +57,7 @@
program = CURL_PROGRAM
cmdline = [program, API_URL, '-s', '-u', '%s:' % args.key, '-X', 'POST']
header_pairs = [['-d', '%s=%s' % (header, data)] for header, data in push_headers.iteritems()]
cmdline += [item.encode() for sublist in header_pairs for item in sublist]
cmdline += [item.encode(DEFAULT_ENCODING) for sublist in header_pairs for item in sublist]
if debug_mode:
cmdline += ['--trace-ascii', TRACE_FILE]
print 'Command line:'
Expand Down

0 comments on commit 8cf920e

Please sign in to comment.