41 lines
1.1 KiB
Python
Executable File
41 lines
1.1 KiB
Python
Executable File
#!/usr/bin/python
|
|
import imaplib, mailbox
|
|
import sys, re, os
|
|
|
|
user=''
|
|
passwd=''
|
|
imap_server = ''
|
|
mailbox = 'Inbox.Sent'
|
|
mbox = "test.mbox"
|
|
|
|
def connectIMAP():
|
|
imap = imaplib.IMAP4_SSL(imap_server)
|
|
imap.login(user, passwd)
|
|
return imap
|
|
|
|
# Scans a mailbox and returns a dictionary of the ids
|
|
def scanMailbox(imap, mailbox):
|
|
messages_by_id = {}
|
|
typ, data = imap.select(mailbox, readonly = True)
|
|
if 'OK' != typ:
|
|
print ("Failed: %s" % (data))
|
|
num_msg = int(data[0])
|
|
for i in range(1, num_msg+1):
|
|
typ, data = imap.fetch(i, '(BODY.PEEK[HEADER.FIELDS (MESSAGE-ID)])')
|
|
if 'OK' != typ:
|
|
print ("FETCH command failed: %s" % (i, data))
|
|
mail_header = data[0][1].strip()
|
|
msg_id = re.compile("^Message\-Id\: (.+)", re.IGNORECASE + re.MULTILINE).match(mail_header).group(1)
|
|
if msg_id not in messages_by_id.keys():
|
|
messages_by_id[msg_id] = i
|
|
print ( "%d messages in %s" % (len(messages_by_id.keys()), mailbox))
|
|
return messages_by_id
|
|
|
|
def downloadMail():
|
|
print ("I'm learnding")
|
|
|
|
if __name__ == '__main__':
|
|
imap = connectIMAP()
|
|
scanMailbox(imap, mailbox)
|
|
downloadMail()
|