From 46687de79174e4ac96508451853cca13eae18995 Mon Sep 17 00:00:00 2001 From: Samuel Ameh Date: Fri, 14 Dec 2018 13:48:24 +0100 Subject: [PATCH 1/2] Modify program and add make portable for both gmail and amazon work mail --- .gitignore | 2 +- .idea/encodings.xml | 4 ++ .idea/gmail-backup.iml | 8 ++++ .idea/misc.xml | 7 ++++ .idea/modules.xml | 8 ++++ .idea/workspace.xml | 7 ++++ dobackup.py | 87 ++++++++++++++++++++++++++++++++++++------ imap_client.py | 0 8 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 .idea/encodings.xml create mode 100644 .idea/gmail-backup.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/workspace.xml create mode 100644 imap_client.py diff --git a/.gitignore b/.gitignore index 03839cb..473359f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *.eml - +backup/ /TEST_CREDS.TXT *.pyc diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/gmail-backup.iml b/.idea/gmail-backup.iml new file mode 100644 index 0000000..728ddb5 --- /dev/null +++ b/.idea/gmail-backup.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3999087 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..67d425f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..b809f03 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/dobackup.py b/dobackup.py index 47781f4..0f1e3d3 100644 --- a/dobackup.py +++ b/dobackup.py @@ -7,7 +7,33 @@ UID_RE = re.compile(r"\d+\s+\(UID (\d+)\)$") FILE_RE = re.compile(r"(\d+).eml$") -GMAIL_FOLDER_NAME = "[Gmail]/All Mail" + + +def set_email_server(): + + print("Hi, Select from the list of Available Email clients below: ") + while True: + try: + email_choice = int(raw_input("1. Gmail\n2. Amazon Work Mail\nEnter Choice: ")) + except ValueError: + print"That is an invalid input, try again" + continue + if email_choice == 1: + email_server = 'imap.gmail.com' + options = ['INBOX', '[Gmail]/All Mail', '[Gmail]/DRAFTS', '[Gmail]/Sent Email'] + break + elif email_choice == 2: + + print('Please enter the IMAP server Address of your amazon imap server') + email_server = raw_input("leave blank for default ('imap.mail.us-west-2.awsapps.com'): ") + email_server = 'imap.mail.us-west-2.awsapps.com' if email_server == '' else email_server + options = ['INBOX', 'Sent Items', 'Drafts'] + break + else: + print('That is an invalid response, please try again') + continue + + return email_server, options def getUIDForMessage(svr, n): @@ -19,11 +45,14 @@ def getUIDForMessage(svr, n): return m.group(1) -def downloadMessage(svr, n, fname): +def download_message(svr, n, dirpath, basename): resp, lst = svr.fetch(n, '(RFC822)') if resp != 'OK': raise Exception("Bad response: %s %s" % (resp, lst)) - f = open(fname, 'w') + if not os.path.exists(dirpath): + os.makedirs(dirpath) + fpath = os.path.join(dirpath, basename) + f = open(fpath, 'w+') f.write(lst[0][1]) f.close() @@ -35,18 +64,50 @@ def UIDFromFilename(fname): def get_credentials(): - user = raw_input("Gmail address: ") - pwd = getpass.getpass("Gmail password: ") + user = raw_input("Email address: ") + pwd = getpass.getpass("Email password: ") return user, pwd +def get_folder_to_backup(options): + max_opt = len(options) + while True: + try: + + print("select folder you want to backup...") + for i in range(1, max_opt + 1): + print('{} -> {}'.format(i, options[i - 1])) + + choice = int(raw_input("choice: ")) + except ValueError: + print"That is an invalid input, try again" + continue + if choice not in range(1, max_opt + 1): + print('That is an invalid response, please try again') + continue + else: + folder_name = options[choice - 1] # selecting folder from options list + break + + return folder_name + + def do_backup(): - svr = imaplib.IMAP4_SSL('imap.gmail.com') + + email_server, options = set_email_server() + svr = imaplib.IMAP4_SSL(email_server, 993) # default imap port 993 user, pwd = get_credentials() - svr.login(user, pwd) + resp = svr.login(user, pwd) + resp = resp[0] + if resp == 'OK': + print('Login successful') + else: + print('There is a fatal error somewhere') - resp, [countstr] = svr.select(GMAIL_FOLDER_NAME, True) - count = int(countstr) + email_folder_name = get_folder_to_backup(options) + + resp, countstr = svr.select(email_folder_name, True) + count = int(countstr[0]) existing_files = os.listdir(".") lastdownloaded = max(UIDFromFilename(f) @@ -67,11 +128,15 @@ def do_backup(): # The download loop for i in range(ungotten, count + 1): uid = getUIDForMessage(svr, i) - print "Downloading %d/%d (UID: %s)" % (i, count, uid) - downloadMessage(svr, i, uid + '.eml') + basename = uid+'.eml' + + dir_path = os.path.join('backup', user, email_folder_name) + print "Downloading %d/%d (UID: %s)" % (i, count, basename) + download_message(svr, i, dir_path, basename) svr.close() svr.logout() + if __name__ == "__main__": do_backup() diff --git a/imap_client.py b/imap_client.py new file mode 100644 index 0000000..e69de29 From 358aa2b0763194096404e24cfe685bc4f045e985 Mon Sep 17 00:00:00 2001 From: Samuel Ameh Date: Fri, 14 Dec 2018 13:50:02 +0100 Subject: [PATCH 2/2] Modify program and add make portable for both gmail and amazon work mail --- .gitignore | 2 +- .idea/encodings.xml | 4 ---- .idea/gmail-backup.iml | 8 -------- .idea/misc.xml | 7 ------- .idea/modules.xml | 8 -------- .idea/workspace.xml | 7 ------- 6 files changed, 1 insertion(+), 35 deletions(-) delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/gmail-backup.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 473359f..77b00f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *.eml backup/ /TEST_CREDS.TXT - +.idea/ *.pyc diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 15a15b2..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/gmail-backup.iml b/.idea/gmail-backup.iml deleted file mode 100644 index 728ddb5..0000000 --- a/.idea/gmail-backup.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 3999087..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 67d425f..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index b809f03..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file