A
A
Artem2016-08-19 18:36:59
Python
Artem, 2016-08-19 18:36:59

How does the mail client understand which emails to download?

I am writing a small IMAP email client in Python for specific purposes.
Faced such a problem: the search by Message-ID does not work (more precisely, it does not always work).
You need to somehow save information about which letters have already been downloaded on your computer, and download only new ones. How can I do that? The UID can change when emails are deleted, so it's not an option.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Dubrovin, 2016-08-19
@ber_enot

Store Folder+UID and hash from message headers. In addition to the Message-ID headers, there are also queue identifiers, so there is practically no chance that the headers will match entirely.
If the letter is transferred to another folder and the Folder+UID changes, take the hash from the headers and look for a letter with a different Folder+UID with the same hash, if so, consider it a duplicate.
Missing Folder+UID mark as deleted and kill at the end of the session.

V
Vladimir, 2016-08-20
@vintello

This code only displays new ones, i.e. unread letters

def get_list_mail():
    server = imaplib.IMAP4_SSL(server_imap, port_imap)
    server.login("*****@mail.ru", password)
    server.select()
    result, ids = server.search(None, '(UNSEEN)')
    print "New emails with your email in TO is %d" % len(ids[0].split())
    for id in ids[0].split():
        subject = server.fetch(id,
                               '(BODY.PEEK[HEADER.FIELDS (SUBJECT)])')[1][0][1].strip()
        print "\t" + quopri.decodestring(subject.encode('utf-8')).decode('utf-8')
    server.close()
    server.logout()

D
Dimonchik, 2016-08-19
@dimonchik2013

move to folder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question