I have a python script called from procmail which does what I want to the email header, but I am missing how to return the modified header back to procmail for continued processing. It is called from procmail like this;


| formail -c | /root/bin/emailfilter.py

I've posted to the python forum with no reply.

The script is;

Code:
#!/usr/bin/python

import os, sys, time, email, email.Utils, MySQLdb, re

def main():
        arg = sys.argv;
        raw_msg = sys.stdin.read()
        p = re.compile("from .*\[([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)\].*by ame[0-9].swcp.com.*")
        emailmsg = email.message_from_string(raw_msg)
        rec = emailmsg.get_all('Received')
        f = open("/root/.blacklisted", 'r')
        #f = open("/tmp/email.txt", 'wb')
        #f.write(raw_msg)
        for v in rec:
                m = p.match(v)
                if m:
                        while 1:
                                line = f.readline().rstrip()
                                if not line:
                                        break
                                if m.group(1) == line:
                                        emailmsg.add_header('X-Blacklisted', m.group(1))
                                        #print 'Match found: ', m.group(1)
                                        break
#               else:
#                       print 'No match'
                #f.write(v + '\n')
        f.close()
        print emailmsg.as_string()
#       print str(emailmsg)
#       print "----------"
#       print emailmsg.get_payload()
#       print str(emailmsg)
#        sys.exit(emailmsg.as_string())
        sys.exit(0)

main()