40 lines
No EOL
1.1 KiB
Python
40 lines
No EOL
1.1 KiB
Python
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 |