From 7eb30b662f1f14e48433ec88a2f2fe0e680ec3d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Fri, 5 May 2023 10:00:08 +0200 Subject: [PATCH] fix mail-plugin this fix load config the right way. now you can install mail-plugin with ```flaschengeist plugin install mail && flaschengeist plugin enable mail``` --- flaschengeist/plugins/message_mail.py | 34 ++++++++++++++++++--------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/flaschengeist/plugins/message_mail.py b/flaschengeist/plugins/message_mail.py index acc00de..978ab0e 100644 --- a/flaschengeist/plugins/message_mail.py +++ b/flaschengeist/plugins/message_mail.py @@ -1,6 +1,7 @@ import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart +from werkzeug.exceptions import InternalServerError from flaschengeist import logger from flaschengeist.models import User @@ -8,23 +9,29 @@ from flaschengeist.plugins import Plugin from flaschengeist.utils.hook import HookAfter from flaschengeist.controller import userController from flaschengeist.controller.messageController import Message +from flaschengeist.config import config class MailMessagePlugin(Plugin): - def __init__(self, entry_point, config): - super().__init__(entry_point, config) - self.server = config["SERVER"] - self.port = config["PORT"] - self.user = config["USER"] - self.password = config["PASSWORD"] - self.crypt = config["CRYPT"] - self.mail = config["MAIL"] + def load(self): + self.config = config.get("mail", None) + if self.config is None: + logger.error("mail was not configured in flaschengeist.toml") + raise InternalServerError + self.server = self.config["SERVER"] + self.port = self.config["PORT"] + self.user = self.config["USER"] + self.password = self.config["PASSWORD"] + self.crypt = self.config["CRYPT"] + self.mail = self.config["MAIL"] @HookAfter("send_message") - def dummy_send(msg): + def dummy_send(msg, *args, **kwargs): + logger.info(f"(dummy_send) Sending message to {msg.receiver}") self.send_mail(msg) def send_mail(self, msg: Message): + logger.debug(f"Sending mail to {msg.receiver}") if isinstance(msg.receiver, User): if not msg.receiver.mail: logger.warning("Could not send Mail, mail missing: {}".format(msg.receiver)) @@ -34,8 +41,13 @@ class MailMessagePlugin(Plugin): recipients = userController.get_user_by_role(msg.receiver) mail = MIMEMultipart() - mail["From"] = self.mail - mail["To"] = ", ".join(recipients) + try: + mail["From"] = self.mail + mail["To"] = ", ".join(recipients) + except Exception as e: + import traceback + + print(traceback.format_exc()) mail["Subject"] = msg.subject mail.attach(MIMEText(msg.message)) if not hasattr(self, "smtp"):