Untested code
This commit is contained in:
commit
73fb5979a4
8 changed files with 155 additions and 0 deletions
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
__pycache__/
|
||||
*.pyc
|
||||
*.swp
|
||||
settings.ini
|
||||
venv/
|
||||
.vscode
|
0
classes/__init__.py
Normal file
0
classes/__init__.py
Normal file
33
classes/config.py
Normal file
33
classes/config.py
Normal file
|
@ -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")
|
40
classes/mission.py
Normal file
40
classes/mission.py
Normal file
|
@ -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
|
27
classes/smtp.py
Normal file
27
classes/smtp.py
Normal file
|
@ -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()
|
12
mission.sample.mmm
Normal file
12
mission.sample.mmm
Normal file
|
@ -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!
|
8
settings.dist.ini
Normal file
8
settings.dist.ini
Normal file
|
@ -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
|
29
worker.py
Normal file
29
worker.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue