parsing email from stdin
Andreas Perstinger
andipersti at gmail.com
Tue Oct 8 13:11:59 EDT 2013
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" <someone at example.com>
To: "me" <me at 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" <someone at example.com>
to: "me" <me at example.com>
subject: something
body: foo bar AÄÖÜĎӅ baz
Bye, Andreas
More information about the Python-list
mailing list