Initial commit

This commit is contained in:
Leander Hutton 2024-02-02 09:59:35 -05:00
commit 3edb5c4963
2 changed files with 43 additions and 0 deletions

3
README Normal file
View File

@ -0,0 +1,3 @@
Python IMAP mail archiver
Set username, password and server in the script and go.

40
mailarchiver Executable file
View File

@ -0,0 +1,40 @@
#!/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()