Jelajahi Sumber

added python module that should handle imap calls

Noah Vogt 3 tahun lalu
induk
melakukan
c247c19671
2 mengubah file dengan 40 tambahan dan 0 penghapusan
  1. 1 0
      .gitignore
  2. 39 0
      tests/mailFunctions.py

+ 1 - 0
.gitignore

@@ -16,3 +16,4 @@
 .cxx
 local.properties
 app/release/
+*__pycache__/*

+ 39 - 0
tests/mailFunctions.py

@@ -0,0 +1,39 @@
+import imaplib
+
+def errorMsgExit(error_msg):
+    print("Error: " + error_msg)
+
+def checkConnection(host, username, password, port):
+    try:
+        connection = imaplib.IMAP4_SSL(host, port)
+        connection.login(username, password)
+        connection.logout()
+        return True
+    except Exception as e:
+        print(str(e))
+        return False
+
+def connect(host, username, password, port):
+    connect = imaplib.IMAP4_SSL(host, port)
+    connect.login(username, password)
+    return connect
+
+def listMailboxes(connection):
+    mailboxes = connection.list()
+    formatted_mailbox_list = []
+
+    for items in mailboxes:
+        if type(items) == list:
+            for raw_box_string in items:
+                box_string = str(raw_box_string)
+                # TODO: handle cases when folder contains subfolders
+                modified_box_string = (box_string[box_string.find('"/" ')+4:-1])
+
+                # strip unneeded "'s surrounding the folder name
+                if modified_box_string.startswith('"') and modified_box_string.endswith('"'):
+                    modified_box_string = modified_box_string[1:-1]
+
+                formatted_mailbox_list.append(modified_box_string)
+
+    connection.logout()
+    return formatted_mailbox_list