import re
import sys
import codecs
import datetime
def parser(filename):
i = 1
for r in codecs.open(filename, 'r'):
if "postfix/smtp" in r and "to=<" in r and "NOQUEUE" not in r:
#print i, r
r1 = ' '.join(r.split()[:3])
r2 = r.split()[5].strip(':')
mail_to = re.findall(r"to=<([^>]*)", r)[0]
mail_domain = re.findall(r"@([^>]*)", r)[0]
mail_relay = re.findall(r"relay=([^\[]*)", r)[0]
if "delay=" not in r:
mail_delay = "0"
else:
mail_delay = re.findall(r"delay=([^,]*)", r)[0]
if "delays=" not in r:
mail_delays = "0"
else:
mail_delays = re.findall(r"delays=([^,]*)", r)[0]
if "dsn=" not in r:
mail_dsn = "0"
else:
mail_dsn = re.findall(r"dsn=([^,]*)", r)[0]
if "from=<" in r and "NOQUEUE" not in r and "status=expired" not in r and "postfix/pickup" not in r:
#print i, r
r1 = ' '.join(r.split()[:3])
r2 = r.split()[5].strip(':')
mail_from = re.findall(r"from=<([^>]*)", r)[0]
if mail_from == '':
mail_from = 'null'
mail_size = re.findall(r"size=([^,]*)", r)[0]
mail_nrcpt = re.findall(r"nrcpt=([^\s]*)", r)[0]
tmp_from = '|'.join([r1, r2, mail_from, mail_size, mail_nrcpt, now])
open('from_result.txt','a').write(tmp_from+"\n")