mailmonster/worker.py

35 lines
803 B
Python
Raw Normal View History

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