#!/usr/bin/python # Mind you, we have only python 2.4.3 on RHEL-5 import imaplib, subprocess, email import email.Parser cmd_string = "/usr/bin/dspam --user dspam --class=spam "\ + "--source=error --deliver=summary --stdout --signature='%s'" hparser = email.Parser.Parser() client = imaplib.IMAP4_SSL("luther.ceplovi.cz") client.login("dspam","hnus") client.select("Public folders/Junk") status, resp = client.search(None, "ALL") messages = resp[0].split() for msgId in messages: # or no .PEEK ... do I want to mark a message as Seen? typ, msg_data = client.fetch(msgId, '(BODY.PEEK[HEADER.FIELDS (SUBJECT FROM X-DSPAM-SIGNATURE)])') headers = hparser.parsestr(msg_data[0][1],headersonly=True) print headers.keys() if 'X-Dspam-Signature' in headers.keys(): print headers['X-Dspam-Signature'] ret = subprocess.Popen(cmd_string \ % headers['X-Dspam-Signature'], shell=True).wait() if ret == 0: typ, response = client.store(msgId, '+FLAGS', r'(\Deleted)') else: raise OSError, "dspam finished with failure code: %d" % ret client.expunge() client.close() client.logout()