From 0b83486ad28049337163c2d441c7cea612c09971 Mon Sep 17 00:00:00 2001 From: Joshua Sangmeister Date: Tue, 12 Mar 2024 11:01:01 +0100 Subject: [PATCH] Fix email typings --- .../action/actions/user/send_invitation_email.py | 5 ++--- openslides_backend/action/mixins/send_email_mixin.py | 7 +++---- tests/system/action/test_send_email_mixin.py | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/openslides_backend/action/actions/user/send_invitation_email.py b/openslides_backend/action/actions/user/send_invitation_email.py index c129beeb3..70a36e1b0 100644 --- a/openslides_backend/action/actions/user/send_invitation_email.py +++ b/openslides_backend/action/actions/user/send_invitation_email.py @@ -180,7 +180,6 @@ def update_instance(self, instance: dict[str, Any]) -> dict[str, Any]: return instance mail_data = self.get_data_from_meeting_or_organization(meeting_id) - from_email: str | Address if users_email_sender := mail_data.get("users_email_sender", "").strip(): if any( x in users_email_sender for x in EmailUtils.SENDER_NAME_FORBIDDEN_CHARS @@ -192,8 +191,8 @@ def update_instance(self, instance: dict[str, Any]) -> dict[str, Any]: ) result["type"] = EmailErrorType.SETTINGS_ERROR return instance - from_email = Address( - users_email_sender, addr_spec=EmailSettings.default_from_email + from_email = str( + Address(users_email_sender, addr_spec=EmailSettings.default_from_email) ) else: from_email = EmailSettings.default_from_email diff --git a/openslides_backend/action/mixins/send_email_mixin.py b/openslides_backend/action/mixins/send_email_mixin.py index a82572991..f66295c32 100644 --- a/openslides_backend/action/mixins/send_email_mixin.py +++ b/openslides_backend/action/mixins/send_email_mixin.py @@ -5,7 +5,6 @@ from collections.abc import Generator from contextlib import contextmanager from datetime import datetime -from email.headerregistry import Address from email.message import EmailMessage from email.utils import format_datetime, make_msgid from typing import Any @@ -111,7 +110,7 @@ def get_mail_connection() -> Generator[smtplib.SMTP | smtplib.SMTP_SSL, None, No @staticmethod def send_email( client: smtplib.SMTP | smtplib.SMTP_SSL, - from_: str | Address, + from_: str, to: str | list[str], subject: str, content: str, @@ -155,7 +154,7 @@ def send_email( ) message["From"] = from_ - message["To"] = to + message["To"] = to if isinstance(to, str) else ", ".join(to) message.preamble = "You will not see this in a MIME-aware mail reader.\n" message.add_header("Subject", subject) message.add_header("Date", format_datetime(datetime.now())) @@ -168,7 +167,7 @@ def send_email( def send_email_safe( client: smtplib.SMTP | smtplib.SMTP_SSL, logger: Any, - from_: str | Address, + from_: str, to: str | list[str], subject: str, content: str, diff --git a/tests/system/action/test_send_email_mixin.py b/tests/system/action/test_send_email_mixin.py index 1f166812f..3b65834b8 100644 --- a/tests/system/action/test_send_email_mixin.py +++ b/tests/system/action/test_send_email_mixin.py @@ -365,7 +365,7 @@ def test_sender_with_name(self) -> None: EmailSettings.connection_security = "NONE" EmailSettings.port = 25 - sender = Address("Name of sender", addr_spec=self.sender) + sender = str(Address("Name of sender", addr_spec=self.sender)) handler = AIOHandler() with AiosmtpdServerManager(handler): with EmailUtils.get_mail_connection() as mail_client: