This commit is contained in:
Kumi 2022-02-08 06:07:26 +00:00
parent 0ba7955b9e
commit a867d538a5
3 changed files with 39 additions and 12 deletions

21
classes/logger.py Normal file
View file

@ -0,0 +1,21 @@
import logging
import threading
from datetime import datetime
class Logger:
@staticmethod
def _format(message: str, severity: str) -> str:
thread = threading.current_thread().name
datestr = str(datetime.now())
return f"{datestr} - {thread} - {severity} - {message}"
def debug(self, message: str) -> None:
print(self.__class__()._format(message, "DEBUG"))
def info(self, message: str) -> None:
print(self.__class__()._format(message, "INFO"))
def error(self, message: str) -> None:
print(self.__class__()._format(message, "ERROR"))

View file

@ -27,14 +27,16 @@ class Mission:
for attachment in self.attachments: for attachment in self.attachments:
try: try:
content = open(attachment, "rb").read() content = open(attachment[0], "rb").read()
message.add_attachment(content) filename = attachment[0].split("/")[-1]
message.add_attachment(content, *(attachment[1].split("/") if len(attachment) > 1 else ["application", "octet-stream"]), filename=filename)
except: except:
raise
pass pass
message["From"] = self.sender message["From"] = self.sender
message["To"] = ",".join(self.recipients) message["To"] = ",".join(self.recipients)
message["Subject"] = self.subject message["Subject"] = self.subject
return message return message

View file

@ -5,8 +5,10 @@ import pathlib
from classes.config import Config from classes.config import Config
from classes.mission import Mission from classes.mission import Mission
from classes.smtp import SMTP from classes.smtp import SMTP
from classes.logger import Logger
logger = Logger()
config = Config.fromFile("settings.ini") config = Config.fromFile("settings.ini")
tempdir = config.getTempDir() tempdir = config.getTempDir()
@ -17,14 +19,16 @@ if not tempdir.exists():
elif not tempdir.is_dir(): elif not tempdir.is_dir():
raise ValueError(f"Temp Dir {str(tempdir)} exists and is not a directory.") raise ValueError(f"Temp Dir {str(tempdir)} exists and is not a directory.")
messages = []
for f in tempdir.glob("*.mmm"):
mission = Mission.fromFile(f)
messages.append(mission.to_message())
f.unlink()
smtp = SMTP.fromConfig(config) smtp = SMTP.fromConfig(config)
for message in messages: for f in tempdir.glob("*.mmm"):
smtp.send_message(message) logger.debug(f"Found file {f}")
try:
mission = Mission.fromFile(f)
message = mission.to_message()
logger.debug("Sending email...")
smtp.send_message(message)
f.unlink()
except Exception as e:
logger.error(f"An error has occurred while processing {f}: {e}")