commit 73fb5979a408d4d677478639cc600bbe6b6ac8b6 Author: Kumi Date: Mon Feb 7 14:54:57 2022 +0100 Untested code diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7ec4721 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +__pycache__/ +*.pyc +*.swp +settings.ini +venv/ +.vscode \ No newline at end of file diff --git a/classes/__init__.py b/classes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/classes/config.py b/classes/config.py new file mode 100644 index 0000000..5b97386 --- /dev/null +++ b/classes/config.py @@ -0,0 +1,33 @@ +import configparser +import pathlib + +class Config: + @classmethod + def fromFile(cls, path): + parser = configparser.ConfigParser() + parser.read(path) + return cls(parser) + + def __init__(self, config): + self._config = config + + def getTempDir(self): + return pathlib.Path(self._config["MONSTER"].get("TempDir", fallback="/tmp/MailMonster/")) + + def getMailServer(self): + return self._config["MONSTER"].get("Server", fallback="localhost") + + def getMailPort(self): + return int(self._config["MONSTER"].get("Port", fallback=0)) + + def getMailSSL(self): + return bool(int(self._config["MONSTER"].get("SSL", fallback=0))) + + def getMailUsername(self): + return self._config["MONSTER"].get("Username") + + def getMailPassword(self): + return self._config["MONSTER"].get("Password") + + def getMailSender(self): + return self._config["MONSTER"].get("Sender") \ No newline at end of file diff --git a/classes/mission.py b/classes/mission.py new file mode 100644 index 0000000..cd2207d --- /dev/null +++ b/classes/mission.py @@ -0,0 +1,40 @@ +import configparser +import json + +from email.message import EmailMessage + +class Mission: + @classmethod + def fromFile(cls, path): + mission = configparser.ConfigParser() + mission.read(path) + + mission = mission["MISSION"] + + return cls(json.loads(mission["Recipients"]), mission.get("Sender"), mission.get("Subject"), mission.get("Content"), json.loads(mission.get("Files", fallback="[]"))) + + def __init__(self, recipients, sender=None, subject=None, text=None, attachments=None): + self.recipients = recipients + self.sender = sender + self.subject = subject + self.text = text + self.attachments = attachments + + def to_message(self): + message = EmailMessage() + + message.set_content(self.text) + + for attachment in self.attachments: + try: + content = open(attachment, "rb").read() + message.add_attachment(content) + + except: + pass + + message["From"] = self.sender + message["To"] = ",".join(self.recipients) + message["Subject"] = self.subject + + return message \ No newline at end of file diff --git a/classes/smtp.py b/classes/smtp.py new file mode 100644 index 0000000..a8081df --- /dev/null +++ b/classes/smtp.py @@ -0,0 +1,27 @@ +import smtplib + + +class SMTP: + @classmethod + def fromConfig(cls, config): + return cls(config.getMailServer(), config.getMailUsername(), config.getMailPassword(), config.getMailSender(), config.getMailPort(), config.getMailSSL()) + + def __init__(self, host, username=None, password=None, sender=None, port=None, ssl=None): + port = 0 if port is None else port + ssl = bool(ssl) + + smtpclass = smtplib.SMTP_SSL if ssl else smtplib.SMTP + + self.connection = smtpclass(host, port) + self.connection.login(username, password) + + self.sender = sender + + def send_message(self, message, *args, **kwargs): + if not message.get("From"): + message["From"] = self.sender + + self.connection.send_message(message, *args, **kwargs) + + def __del__(self): + self.connection.close() \ No newline at end of file diff --git a/mission.sample.mmm b/mission.sample.mmm new file mode 100644 index 0000000..e3c4460 --- /dev/null +++ b/mission.sample.mmm @@ -0,0 +1,12 @@ +[MISSION] +Files = [ + "/location/file1", + "/location/file2" + ] +Recipients = [ "recipient@example.com" ] +Subject = Your Requested Files +Content = Hello, + + this is your message. + + Best regards! \ No newline at end of file diff --git a/settings.dist.ini b/settings.dist.ini new file mode 100644 index 0000000..a5341db --- /dev/null +++ b/settings.dist.ini @@ -0,0 +1,8 @@ +[MONSTER] +TempDir = /tmp/MailMonster/ +Sender = monster@example.com +Server = example.com +Username = monster@example.com +Password = dkafjöfi925hqojtwkb +SSL = 1 +Port = 465 \ No newline at end of file diff --git a/worker.py b/worker.py new file mode 100644 index 0000000..2911cc2 --- /dev/null +++ b/worker.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import pathlib + +from classes.config import Config +from classes.mission import Mission +from classes.smtp import SMTP + + +config = Config("settings.ini") + +tempdir = pathlib.Path(config["MONSTER"].get("TempDir", "/tmp/MailMonster/")) + +if not tempdir.exists(): + tempdir.mkdir() + +elif not tempdir.isdir(): + 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()) + +smtp = SMTP.fromConfig(config) + +for message in messages: + smtp.send_message(message) \ No newline at end of file