Set header from IMAP

L

Laszlo Nagy

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

IMAP Problems 0
IMAP Checking Folder Size 1
IMAP Folder Size Information 0
IMAP SEARCH Error 2
IMAP Tkinter Issue 0
IMAP mailwatcher w/Tkinter 0
Memory errors with imaplib 6
IMAP4 and deleting a message 1

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top