diff --git a/app/email_utils.py b/app/email_utils.py index 742806de..221f7d63 100644 --- a/app/email_utils.py +++ b/app/email_utils.py @@ -1345,17 +1345,16 @@ def get_queue_id(msg: Message) -> Optional[str]: received_header = str(msg[headers.RECEIVED]) if not received_header: - return + return None # received_header looks like 'from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434])\r\n\t(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits))\r\n\t(No client certificate requested)\r\n\tby mx1.simplelogin.co (Postfix) with ESMTPS id 4FxQmw1DXdz2vK2\r\n\tfor ; Fri, 4 Jun 2021 14:55:43 +0000 (UTC)' - search_result = re.search("with ESMTPS id [0-9a-zA-Z]{1,}", received_header) - if not search_result: - return - - # the "with ESMTPS id 4FxQmw1DXdz2vK2" part - with_esmtps = received_header[search_result.start() : search_result.end()] - - return with_esmtps[len("with ESMTPS id ") :] + search_result = re.search(r"with E?SMTP[AS]? id ([0-9a-zA-Z]{1,})", received_header) + if search_result: + return search_result.group(1) + search_result = re.search("\(Postfix\)\r\n\tid ([a-zA-Z0-9]{1,});", received_header) + if search_result: + return search_result.group(1) + return None def should_ignore_bounce(mail_from: str) -> bool: diff --git a/tests/test_email_utils.py b/tests/test_email_utils.py index c2cc8ab9..cf498e13 100644 --- a/tests/test_email_utils.py +++ b/tests/test_email_utils.py @@ -791,12 +791,21 @@ def test_parse_id_from_bounce(): assert parse_id_from_bounce("anything+1234+@local") == 1234 -def test_get_queue_id(): +def test_get_queue_id_esmtps(): + for id_type in ["SMTP", "ESMTP", "ESMTPA", "ESMTPS"]: + msg = email.message_from_string( + f"Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434])\r\n\t(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits))\r\n\t(No client certificate requested)\r\n\tby mx1.simplelogin.co (Postfix) with {id_type} id 4FxQmw1DXdz2vK2\r\n\tfor ; Fri, 4 Jun 2021 14:55:43 +0000 (UTC)" + ) + + assert get_queue_id(msg) == "4FxQmw1DXdz2vK2", f"Failed for {id_type}" + + +def test_get_queue_id_postfix(): msg = email.message_from_string( - "Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434])\r\n\t(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits))\r\n\t(No client certificate requested)\r\n\tby mx1.simplelogin.co (Postfix) with ESMTPS id 4FxQmw1DXdz2vK2\r\n\tfor ; Fri, 4 Jun 2021 14:55:43 +0000 (UTC)" + "Received: by mailin001.somewhere.net (Postfix)\r\n\tid 4Xz5pb2nMszGrqpL; Wed, 27 Nov 2024 17:21:59 +0000 (UTC)'] by mailin001.somewhere.net (Postfix)" ) - assert get_queue_id(msg) == "4FxQmw1DXdz2vK2" + assert get_queue_id(msg) == "4Xz5pb2nMszGrqpL" def test_get_queue_id_from_double_header():