hopefully not breaking too much...

pull/4/head
Sascha Rommelfangen 2017-06-01 18:39:39 +02:00
parent 3de6a6a5aa
commit e1ade5b8d7
1 changed files with 39 additions and 30 deletions

View File

@ -54,17 +54,22 @@ debug = config.debug
stdin_used = False stdin_used = False
email_subject = config.email_subject_prefix email_subject = config.email_subject_prefix
try: #try:
if not sys.stdin.isatty(): #if not sys.stdin.isatty():
if len(sys.argv) == 1:
mailcontent = sys.stdin.buffer.read().decode("utf-8", "ignore") mailcontent = sys.stdin.buffer.read().decode("utf-8", "ignore")
else: else:
mailcontent = sys.argv[1] mailcontent = sys.argv[1]
syslog.syslog(mailcontent)
if len(sys.argv) >= 3: if len(sys.argv) >= 3:
mail_subject = sys.argv[2] mail_subject = sys.argv[2]
email_data = b'' email_data = b''
syslog.syslog(mailcontent)
msg = email.message_from_string(mailcontent) msg = email.message_from_string(mailcontent)
if not mail_subject:
try:
mail_subject = msg.get('Subject').encode("utf-8", "ignore") mail_subject = msg.get('Subject').encode("utf-8", "ignore")
except:
pass
for part in msg.walk(): for part in msg.walk():
if part.get_content_charset() is None: if part.get_content_charset() is None:
# This could probably be detected # This could probably be detected
@ -75,16 +80,20 @@ try:
continue continue
if part.get_content_maintype() == 'text': if part.get_content_maintype() == 'text':
part.set_charset(charset) part.set_charset(charset)
try:
syslog.syslog(str(part.get_payload(decode=True))
except Exception as e:
syslog.syslog(str(e))
email_data += part.get_payload(decode=True) email_data += part.get_payload(decode=True)
print("here") print("here")
email_subject += mail_subject email_subject += mail_subject
stdin_used = True stdin_used = True
except Exception as e: #except Exception as e:
if debug: # if debug:
syslog.syslog("FATAL ERROR: Not all required input received") # syslog.syslog("FATAL ERROR: Not all required input received")
print(str(e)) # print(str(e))
syslog.syslog(str(e)) # syslog.syslog(str(e))
sys.exit(1) # sys.exit(1)
#if debug: #if debug:
# syslog.syslog("Encoding of subject: {0}".format(ftfy.guess_bytes(email_subject)[1])) # syslog.syslog("Encoding of subject: {0}".format(ftfy.guess_bytes(email_subject)[1]))