From 3edb5c4963e6f9552b7f85715e27bb6a8850585f Mon Sep 17 00:00:00 2001 From: Leander Hutton Date: Fri, 2 Feb 2024 09:59:35 -0500 Subject: [PATCH] Initial commit --- README | 3 +++ mailarchiver | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 README create mode 100755 mailarchiver diff --git a/README b/README new file mode 100644 index 0000000..27612b1 --- /dev/null +++ b/README @@ -0,0 +1,3 @@ +Python IMAP mail archiver + +Set username, password and server in the script and go. diff --git a/mailarchiver b/mailarchiver new file mode 100755 index 0000000..126ad80 --- /dev/null +++ b/mailarchiver @@ -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()