Search code examples
pythonpop3poplib

Fetch mails via POP3, but keep them on the server


I'd like to fetch mails from a server, but I also want to control when to delete them.

Is there a way to do this?

I know this setting is very usual in mail clients, but it seems this option is not well supported by POPv3 specification and/or server implementations.

(I'm using python, but I'm ok with other languages/libraries, Python's poplib seems very simplistic)


Solution

  • Most POP3 clients may delete successfully retrieved messages automatically, but that's a feature of the client itself, not the protocol. POPv3 supports four basic operations during the transaction phase of a session:

    1. Listing all available messages in the mailbox. (LIST)
    2. Retrieving a specific message (RETR)
    3. Flagging a message for deletion (DELE)
    4. Clearing all deletion flags (RSET)

    After the client ends the session with the QUIT command, any messages still flagged for deletion are deleted during the update phase. Note, though, that the RETR command (based on my reading of RFC1939 does not flag a message for deletion; that needs to be done explicitly with the DELE command.

    Note, however, that a particular POP3 server may have a policy of deleting retrieved messages, whether or not the client requested they be deleted. Whether such a server provides an operation to bypass that is beyond the scope of the protocol. (A discussion of this point is mentioned in section 8 of the RFC, but is not part of the protocol itself.)