From d8028c46812e7045706f6c59966095cc54e0c09c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Tue, 9 May 2023 21:25:19 +0200 Subject: [PATCH] fixed timeout in mailing #30 --- flaschengeist/plugins/message_mail.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/flaschengeist/plugins/message_mail.py b/flaschengeist/plugins/message_mail.py index 978ab0e..ecaa042 100644 --- a/flaschengeist/plugins/message_mail.py +++ b/flaschengeist/plugins/message_mail.py @@ -31,7 +31,7 @@ class MailMessagePlugin(Plugin): self.send_mail(msg) def send_mail(self, msg: Message): - logger.debug(f"Sending mail to {msg.receiver}") + logger.debug(f"Sending mail to {msg.receiver} with subject {msg.subject}") if isinstance(msg.receiver, User): if not msg.receiver.mail: logger.warning("Could not send Mail, mail missing: {}".format(msg.receiver)) @@ -41,18 +41,12 @@ class MailMessagePlugin(Plugin): recipients = userController.get_user_by_role(msg.receiver) mail = MIMEMultipart() - try: - mail["From"] = self.mail - mail["To"] = ", ".join(recipients) - except Exception as e: - import traceback - - print(traceback.format_exc()) + mail["From"] = self.mail + mail["To"] = ", ".join(recipients) mail["Subject"] = msg.subject mail.attach(MIMEText(msg.message)) - if not hasattr(self, "smtp"): - self.__connect() - self.smtp.sendmail(self.mail, recipients, mail.as_string()) + with self.__connect() as smtp: + smtp.sendmail(self.mail, recipients, mail.as_string()) def __connect(self): if self.crypt == "SSL": @@ -63,3 +57,4 @@ class MailMessagePlugin(Plugin): else: raise ValueError("Invalid CRYPT given") self.smtp.login(self.user, self.password) + return self.smtp