On 08.10.2013 17:25, Antoon Pardon wrote:
Op 08-10-13 16:24, Andreas Perstinger schreef:
Looking at the docs, I've found there is also "message_from_binary_file"
which works for me with your code.

http://docs.python.org/3/library/email.parser.html#email.message_from_binary_file


I can't try that out right now, but I had a look at the code and the
ByteParser that is mentioned their looks like this:

class BytesFeedParser(FeedParser):
      """Like FeedParser, but feed accepts bytes."""

      def feed(self, data):
          super().feed(data.decode('ascii', 'surrogateescape'))


Somehow I doubt that trying to decode my utf-8 stream as if it was
ascii will work.

Actually it does work:

$ cat testmail.txt
From: "someone" <some...@example.com>
To: "me" <m...@example.com>
Subject: something
Content-Type: text/plain; charset="UTF-8";
Content-Transfer-Encoding: 8bit

foo bar AÄÖÜĎӅ baz

$ file testmail.txt
testmail.txt: news or mail, UTF-8 Unicode text

$ cat foo.py
#!/usr/bin/python3

import sys
from email import message_from_binary_file

sys.stdin = sys.stdin.detach()
msg = message_from_binary_file(sys.stdin)

print("from: ", msg['From'])
print("to: ", msg['To'])
print("subject: ", msg['Subject'])
print("body: ", msg.get_payload())

$ ./foo.py < testmail.txt
from:  "someone" <some...@example.com>
to:  "me" <m...@example.com>
subject:  something
body:  foo bar AÄÖÜĎӅ baz

Bye, Andreas
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to