mailmonster/worker.py
2022-02-08 06:07:26 +00:00

34 lines
803 B
Python

#!/usr/bin/env python3
import pathlib
from classes.config import Config
from classes.mission import Mission
from classes.smtp import SMTP
from classes.logger import Logger
logger = Logger()
config = Config.fromFile("settings.ini")
tempdir = config.getTempDir()
if not tempdir.exists():
tempdir.mkdir()
elif not tempdir.is_dir():
raise ValueError(f"Temp Dir {str(tempdir)} exists and is not a directory.")
smtp = SMTP.fromConfig(config)
for f in tempdir.glob("*.mmm"):
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}")