mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

commit 89c493929770bcbd28f6f93dc63bb37bc606ddcc
parent 9c65e28849b4709dcb79d5b78d62e89b1977a690
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sat, 23 Nov 2019 15:23:21 +0100

get_email_header

Diffstat:
get_email_header | 22++++++++++++++++++++++
1 file changed, 22 insertions(+), 0 deletions(-)

diff --git a/get_email_header b/get_email_header @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +# adapted from https://stackoverflow.com/a/39464990/414272 + +from email.parser import BytesParser +from email.header import Header, decode_header +from email.errors import HeaderParseError +from sys import argv, stdin + +header = argv[1] + +message = BytesParser().parse(stdin.buffer) +try: + subj = ''.join([ + frag.decode(enc if enc else "utf-8") + if isinstance(frag, bytes) else frag + for frag, enc in decode_header(message['subject'])]) +except (HeaderParseError, UnicodeDecodeError): # maybe warn about error? + subj = message['subject'] + subj = subj.decode("utf-8") if isinstance(subj, bytes) else subj +print(subj) + +