Dear List, I'm trying to figure out, how to change the header of an e-mail that is accessed by an IMAP object. There is some code below (not a complete program). The key function is the filter_mail function. It should move/delete/change/export messages, based on their header. For exporting messages, I use this: "typ,dat = self.conn.uid('FETCH',self.uid,'(RFC822)') ". The only thing I don't know how to do, is to insert/modify/delete custom headers in e-mails. I was looking for this on Google and the Python cookbook, but I did not find anyting useful. Please help.
Laszlo import local import imaplib class IMAPError(Exception): pass def check_imap_error((typ,data)): if (typ != 'OK'): raise IMAPError((typ,data)) def getnewimapconnection(): conn = imaplib.IMAP4_SSL(local.IMAP_HOST) check_imap_error(conn.LOGIN(local.IMAP_LOGIN,local.IMAP_PWD)) return conn conn = getnewimapconnection() cnt = 0 try: for inputfolder in (local.SOURCE_FOLDER,local.SOURCE_FOLDER2): typ,data = conn.select(inputfolder) check_imap_error((typ,data)) cnt = int(data[0]) if cnt > 0: self.logger.info("Processing %s messages in %s",cnt,inputfolder) # Get UID and RFC822 headers for all messages typ, items = conn.fetch('1:%s'%cnt, '(UID RFC822.HEADER)') check_imap_error((typ, items)) for item in items: if isinstance(item,tuple): uid = item[0].split()[2] headers = item[1] parser = email.Parser.Parser() parsed = parser.parsestr(headers,True) filter_mail(conn,uid,parsed) conn.expunge() else: self.logger.info("No new messages in %s",inputfolder) finally: conn.logout() -- http://mail.python.org/mailman/listinfo/python-list