瀏覽代碼

add cc / bcc functionality + do some string beautification for fetchMails function

Noah Vogt 3 年之前
父節點
當前提交
195d079410
共有 2 個文件被更改,包括 38 次插入6 次删除
  1. 0 2
      app/src/main/python/helloworldscript.py
  2. 38 4
      app/src/main/python/mailFunctions.py

+ 0 - 2
app/src/main/python/helloworldscript.py

@@ -1,2 +0,0 @@
-def hi():
-    return "python says hi"

+ 38 - 4
app/src/main/python/mailFunctions.py

@@ -1,4 +1,28 @@
 import imaplib, smtplib, ssl, email, os
+from itertools import chain
+
+# format raw string you get from fetching mails
+def stringCompiling(inputIterable):
+    # remove first nested iterables
+    try:
+        unitered = list(chain.from_iterable(inputIterable))
+    except TypeError:
+        unitered = inputIterable
+
+    # remove None Type entries
+    nonNoneList = []
+    try:
+        for item in unitered:
+            if item is not None:
+                nonNoneList.append(item)
+    except TypeError:
+        return ""
+
+    # return non empty values
+    if len(nonNoneList) >= 1:
+        return nonNoneList[0]
+    else:
+        return ""
 
 def errorMsgExit(error_msg):
     print("Error: " + error_msg)
@@ -65,7 +89,15 @@ def fetchMails(connection, inbox):
         try:
             raw_to = email.header.decode_header(msg['To'])
         except TypeError:
-            raw_to = [("", None)]
+            raw_to = [""]
+        try:
+            raw_cc = email.header.decode_header(msg['CC'])
+        except TypeError:
+            raw_cc = [""]
+        try:
+            raw_bcc = email.header.decode_header(msg['BCC'])
+        except TypeError:
+            raw_bcc = [""]
         print("raw_to" + str(raw_to))
         raw_date = email.header.decode_header(msg['Date'])[0]
         print("raw_to" + str(raw_date))
@@ -88,9 +120,11 @@ def fetchMails(connection, inbox):
         #print("subject: {}".format(subject))
 
         output_dict['subject'] = subject
-        output_dict['from'] = raw_from[0]
-        output_dict['to'] = raw_to[0]
-        output_dict['date'] = raw_date[0]
+        output_dict['from'] = stringCompiling(raw_from)
+        output_dict['cc'] = stringCompiling(raw_cc)
+        output_dict['bcc'] = stringCompiling(raw_bcc)
+        output_dict['to'] = stringCompiling(raw_to)
+        output_dict['date'] = stringCompiling(raw_date)
         output_dict['content'] = primitive_body
 
         output_list.append(output_dict)