70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
import pprint
|
|
import os
|
|
import json
|
|
import base64
|
|
import os
|
|
import email
|
|
import zipfile
|
|
import gzip
|
|
from imaplib import IMAP4, IMAP4_SSL
|
|
|
|
download_folder = ""
|
|
host = ""
|
|
username = ""
|
|
password = ""
|
|
|
|
if not os.path.isdir(download_folder):
|
|
os.makedirs(download_folder, exist_ok=True)
|
|
|
|
with IMAP4(host) as mail:
|
|
mail.starttls()
|
|
mail.login(username, password)
|
|
mail.select("INBOX.DMARC")
|
|
type, data = mail.search(None, "UNSEEN")
|
|
mail_ids = data[0]
|
|
id_list = mail_ids.split()
|
|
for num in data[0].split():
|
|
typ, data = mail.fetch(num, "(RFC822)")
|
|
raw_email = data[0][1] # converts byte literal to string removing b''
|
|
raw_email_string = raw_email.decode("utf-8")
|
|
email_message = email.message_from_string(
|
|
raw_email_string
|
|
) # downloading attachments
|
|
for part in email_message.walk():
|
|
# this part comes from the snipped I don't understand yet...
|
|
if part.get_content_maintype() == "multipart":
|
|
continue
|
|
if part.get("Content-Disposition") is None:
|
|
continue
|
|
fileName = part.get_filename()
|
|
if bool(fileName):
|
|
filePath = os.path.join("/tmp", fileName)
|
|
if not os.path.isfile(filePath):
|
|
fp = open(filePath, "wb")
|
|
fp.write(part.get_payload(decode=True))
|
|
fp.close()
|
|
print(
|
|
'Downloaded "{file}" (UID {uid}).'.format(
|
|
file=fileName, uid=num
|
|
)
|
|
)
|
|
filename, file_extension = os.path.splitext(filePath)
|
|
if file_extension == ".zip":
|
|
print(f"extract {filename} (zip)")
|
|
with zipfile.ZipFile(filePath,"r") as zip_ref:
|
|
zip_ref.extractall(download_folder)
|
|
elif file_extension == ".gz":
|
|
print(f"extract {filename} (gz)")
|
|
with gzip.open(filePath, 'rb') as gzip_file:
|
|
file_content = gzip_file.read()
|
|
file = os.path.basename(filename)
|
|
fileFolder = os.path.join(download_folder, file)
|
|
print(f"write {file} in {fileFolder}")
|
|
fp = open(fileFolder, "wb")
|
|
fp.write(file_content)
|
|
fp.close()
|
|
else:
|
|
print(f"{file_extension} is not a supported extension")
|
|
os.remove(filePath)
|
|
mail.noop()
|