mailmonster/classes/mission.py

40 lines
1.1 KiB
Python
Raw Normal View History

2022-02-07 13:54:57 +00:00
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