views:

214

answers:

2

Is there an easy way to add a custom RFC822 header to a message on an IMAP server with imaplib?

I am writing a python-based program that filters my IMAP mail store. When I did this with Procmail I had the option of adding headers. But there doesn't seem to be a way to do that with the Python imap implementation.

Specifically, I want to add a custom header like:

X-VY32-STATUS: Very Cool

So that it appears in the mail headers:

 To: [email protected]
 From: [email protected]
 Subject: Test Message
 X-VY32-STATUS: Very Cool

 The regular message is down here.
A: 

I don't think RFC 2060 allows the edit of received mail headers. If you want to edit the mail response header this can be done with the email package.

zoli2k
So my only option is to download the header, modify it, and send it back?
vy32
Procmail is a mail delivery agent. The IMAP protocol is used to transfer the mails form a mail server and not for editing mails on the server.
zoli2k
Yes, I know that procmail is a delivery agent. But increasingly we are running with mailservers that are only accessible by IMAP. It would be nice to have procmail-like functionality from a python script running on a client somewhere. There are some systems that do this (not particularly well, I might add). Right now they don't have the ability to update the mail headers. There would be a nice way to do that. You can do it with IMAP by downloading the entire message and then writing it back, but that's not very efficient.
vy32
A: 

Better option would be to use custom server flags called keywords.

A keyword is defined by the server implementation. Keywords do not begin with "\". Servers MAY permit the client to define new keywords in the mailbox.

To add myflag to the message you can use

STORE number +FLAGS (myflag) 

to search:

SEARCH KEYWORD myflag

Bear in mind that some servers don't allow custom flags.

Pawel Lesnikowski
But I'm not sure that the Apple Mail client lets me access the flags.
vy32
Unfortunately, Apple Mail client does not let me access the flags. However, you are correct, this is the correct way to do it.
vy32