|
@@ -1,8 +1,13 @@
|
|
-import imaplib, smtplib, ssl, email, os, json
|
|
|
|
|
|
+import imaplib
|
|
|
|
+import smtplib
|
|
|
|
+import ssl
|
|
|
|
+import email
|
|
|
|
+import json
|
|
from itertools import chain
|
|
from itertools import chain
|
|
|
|
|
|
-# format raw string you get from fetching mails
|
|
|
|
|
|
+
|
|
def stringCompiling(inputIterable):
|
|
def stringCompiling(inputIterable):
|
|
|
|
+ # format raw string you get from fetching mails
|
|
# remove first nested iterables
|
|
# remove first nested iterables
|
|
try:
|
|
try:
|
|
unitered = list(chain.from_iterable(inputIterable))
|
|
unitered = list(chain.from_iterable(inputIterable))
|
|
@@ -20,10 +25,6 @@ def stringCompiling(inputIterable):
|
|
except UnicodeDecodeError:
|
|
except UnicodeDecodeError:
|
|
nonNoneList.append(str(item.decode("iso-8859-1")))
|
|
nonNoneList.append(str(item.decode("iso-8859-1")))
|
|
except AttributeError:
|
|
except AttributeError:
|
|
- #print(item)
|
|
|
|
- #print(type(item))
|
|
|
|
- #print(inputIterable)
|
|
|
|
- #print(nonNoneList)
|
|
|
|
exit()
|
|
exit()
|
|
else:
|
|
else:
|
|
nonNoneList.append(item)
|
|
nonNoneList.append(item)
|
|
@@ -37,9 +38,11 @@ def stringCompiling(inputIterable):
|
|
else:
|
|
else:
|
|
return ""
|
|
return ""
|
|
|
|
|
|
|
|
+
|
|
def errorMsgExit(error_msg):
|
|
def errorMsgExit(error_msg):
|
|
print("Error: " + error_msg)
|
|
print("Error: " + error_msg)
|
|
|
|
|
|
|
|
+
|
|
def checkConnection(host, username, password, port):
|
|
def checkConnection(host, username, password, port):
|
|
try:
|
|
try:
|
|
connection = imaplib.IMAP4_SSL(host, port)
|
|
connection = imaplib.IMAP4_SSL(host, port)
|
|
@@ -56,6 +59,7 @@ def checkConnection(host, username, password, port):
|
|
|
|
|
|
return False
|
|
return False
|
|
|
|
|
|
|
|
+
|
|
def connect(host, username, password, port):
|
|
def connect(host, username, password, port):
|
|
connect = imaplib.IMAP4_SSL(host, port)
|
|
connect = imaplib.IMAP4_SSL(host, port)
|
|
connect.login(username, password)
|
|
connect.login(username, password)
|
|
@@ -65,6 +69,7 @@ def connect(host, username, password, port):
|
|
pass
|
|
pass
|
|
return connect
|
|
return connect
|
|
|
|
|
|
|
|
+
|
|
def listMailboxes(connection):
|
|
def listMailboxes(connection):
|
|
mailboxes = connection.list()
|
|
mailboxes = connection.list()
|
|
formatted_mailbox_list = []
|
|
formatted_mailbox_list = []
|
|
@@ -74,21 +79,20 @@ def listMailboxes(connection):
|
|
for raw_box_string in items:
|
|
for raw_box_string in items:
|
|
box_string = str(raw_box_string)
|
|
box_string = str(raw_box_string)
|
|
# TODO: handle cases when folder contains subfolders
|
|
# TODO: handle cases when folder contains subfolders
|
|
- modified_box_string = (box_string[box_string.find('"/" ')+4:-1])
|
|
|
|
|
|
+ modified_box = (box_string[box_string.find('"/" ')+4:-1])
|
|
|
|
|
|
# strip unneeded "'s surrounding the folder name
|
|
# 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]
|
|
|
|
|
|
+ if modified_box.startswith('"') and modified_box.endswith('"'):
|
|
|
|
+ modified_box = modified_box[1:-1]
|
|
|
|
|
|
- formatted_mailbox_list.append(modified_box_string)
|
|
|
|
|
|
+ formatted_mailbox_list.append(modified_box)
|
|
|
|
|
|
connection.logout()
|
|
connection.logout()
|
|
return formatted_mailbox_list
|
|
return formatted_mailbox_list
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
- # check that there are no bytes anymore that cannot be dumped into a json
|
|
|
|
def verifyNoBytes(messages, output_list):
|
|
def verifyNoBytes(messages, output_list):
|
|
|
|
+ # check that there are no bytes anymore that cannot be dumped into a json
|
|
for messages in output_list:
|
|
for messages in output_list:
|
|
for item in messages:
|
|
for item in messages:
|
|
#print(type(item))
|
|
#print(type(item))
|
|
@@ -104,6 +108,7 @@ def verifyNoBytes(messages, output_list):
|
|
print("ERROREXIT")
|
|
print("ERROREXIT")
|
|
exit()
|
|
exit()
|
|
|
|
|
|
|
|
+
|
|
def fetchMails(connection, inbox, folderLocal):
|
|
def fetchMails(connection, inbox, folderLocal):
|
|
#print("###" + inbox + "###")
|
|
#print("###" + inbox + "###")
|
|
#print(type(inbox))
|
|
#print(type(inbox))
|
|
@@ -115,8 +120,6 @@ def fetchMails(connection, inbox, folderLocal):
|
|
#print("status-------\n" + status)
|
|
#print("status-------\n" + status)
|
|
#print("messages-------\n" + str(messages))
|
|
#print("messages-------\n" + str(messages))
|
|
|
|
|
|
- # number of top emails to fetch
|
|
|
|
- #N = 3
|
|
|
|
# total number of emails
|
|
# total number of emails
|
|
messages_int = int(messages[0])
|
|
messages_int = int(messages[0])
|
|
#print("message_int------\n" + str(messages_int))
|
|
#print("message_int------\n" + str(messages_int))
|
|
@@ -189,31 +192,29 @@ def fetchMails(connection, inbox, folderLocal):
|
|
else:
|
|
else:
|
|
output_dict['seen'] = "False"
|
|
output_dict['seen'] = "False"
|
|
# make sure the fetch command doesn't add a SEEN flag
|
|
# make sure the fetch command doesn't add a SEEN flag
|
|
- connection.store(num, '-FLAGS', '(\Seen)')
|
|
|
|
|
|
+ connection.store(num, '-FLAGS', '(\\Seen)')
|
|
|
|
|
|
output_list.append(output_dict)
|
|
output_list.append(output_dict)
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
connection.close()
|
|
connection.close()
|
|
connection.logout()
|
|
connection.logout()
|
|
|
|
|
|
verifyNoBytes(messages, output_list)
|
|
verifyNoBytes(messages, output_list)
|
|
|
|
|
|
- print("Finstep")
|
|
|
|
-
|
|
|
|
return json.dumps(output_list)
|
|
return json.dumps(output_list)
|
|
|
|
|
|
|
|
|
|
-def sendStarttls(host, sendingMail, receivingMail, password, message="", subject="", port=587, cc=[], bcc=[]):
|
|
|
|
|
|
+def sendStarttls(host, sendingMail, receivingMail, password, message="",
|
|
|
|
+ subject="", port=587, cc=[], bcc=[]):
|
|
context = ssl.create_default_context()
|
|
context = ssl.create_default_context()
|
|
|
|
|
|
if type(cc) is not str:
|
|
if type(cc) is not str:
|
|
cc = ",".join(cc)
|
|
cc = ",".join(cc)
|
|
if type(bcc) is not str:
|
|
if type(bcc) is not str:
|
|
bcc = ",".join(bcc)
|
|
bcc = ",".join(bcc)
|
|
- utf8Message = "Subject: " + subject + "\nCC: " + cc + "\nBCC: " + bcc + "\n\n" + message
|
|
|
|
- decoded=utf8Message.encode('cp1252').decode('utf-8')
|
|
|
|
|
|
+ utf8Message = ("Subject: " + subject + "\nCC: " + cc + "\nBCC: " + bcc +
|
|
|
|
+ "\n\n" + message)
|
|
|
|
+ decoded = utf8Message.encode('cp1252').decode('utf-8')
|
|
|
|
|
|
with smtplib.SMTP(host, port) as serverConnection:
|
|
with smtplib.SMTP(host, port) as serverConnection:
|
|
serverConnection.starttls(context=context)
|
|
serverConnection.starttls(context=context)
|