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[0], "rb").read() filename = attachment[0].split("/")[-1] message.add_attachment(content, *(attachment[1].split("/") if len(attachment) > 1 else ["application", "octet-stream"]), filename=filename) except: raise pass message["From"] = self.sender message["To"] = ",".join(self.recipients) message["Subject"] = self.subject return message