Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(ses): replace existing Message-ID in SendRawEmail SMTP relay (#2073)
kjdevef46eed1a257
diff --git a/ministack/services/ses.py b/ministack/services/ses.pyindex 4c07478a..58731405 100644--- a/ministack/services/ses.py+++ b/ministack/services/ses.py@@ -618,7 +618,8 @@ def _send_raw_email(params):decoded = base64.b64decode(raw_bytes)except Exception:decoded = raw_bytes- raw_str = f'Message-ID: <{msg_id}>\r\n' + decoded.decode('utf-8', errors='replace')+ raw_str = f'Message-ID: <{msg_id}>\r\n' + _strip_message_id_header(+ decoded.decode('utf-8', errors='replace'))_smtp_relay(actual_source, relay_addrs, raw_str)except Exception:logger.warning('SMTP relay failed for SendRawEmail: %s', msg_id, exc_info=True)@@ -1252,6 +1253,27 @@ def _build_mime_message(source, to_addrs, cc_addrs, bcc_addrs,_SMTP_TIMEOUT_SECONDS = 10+def _strip_message_id_header(raw_str):+ """Drop client-supplied Message-ID headers (incl. folded lines); real SES replaces it."""+ lines = raw_str.splitlines(keepends=True)+ out = []+ in_headers = True+ skipping = False+ for line in lines:+ if in_headers:+ if line in ('\r\n', '\n', '\r'):+ in_headers = False+ elif line[0] in ' \t':+ if skipping:+ continue+ else:+ skipping = line.lower().startswith('message-id:')+ if skipping:+ continue+ out.append(line)+ return ''.join(out)++def _smtp_relay(source, to_addrs, message_str):"""Relay email via external SMTP if SMTP_HOST is set. Best-effort, off the event loop."""endpoint = _parse_smtp_host()diff --git a/tests/test_ses.py b/tests/test_ses.pyindex 6f7b4f9b..7f78855a 100644--- a/tests/test_ses.py+++ b/tests/test_ses.py@@ -511,6 +511,40 @@ def test_ses_smtp_relay_send_raw_email(monkeypatch):assert '[email protected]' in call_args[0][1]+def test_ses_smtp_relay_send_raw_email_replaces_existing_message_id(monkeypatch):+ _verify_example_com(monkeypatch)+ monkeypatch.setenv('SMTP_HOST', 'localhost:2525')+ from ministack.services.ses import _send_raw_email+ mock_smtp = MagicMock()+ with patch('ministack.services.ses.smtplib.SMTP', return_value=mock_smtp):+ mock_smtp.__enter__ = MagicMock(return_value=mock_smtp)+ mock_smtp.__exit__ = MagicMock(return_value=False)+ raw_msg = (+ 'From: [email protected]\r\n'+ 'Message-ID:\r\n'+ ' <[email protected]>\r\n'+ 'To: [email protected]\r\n'+ 'Subject: Raw Test\r\n'+ '\r\n'+ 'Message-ID: <[email protected]>\r\n'+ )+ params = {+ 'Source': ['[email protected]'],+ 'Destinations.member.1': ['[email protected]'],+ 'RawMessage.Data': [raw_msg],+ }+ status, _, _ = _send_raw_email(params)+ assert status == 200+ sent = mock_smtp.sendmail.call_args[0][2]+ header_part, body = sent.split('\r\n\r\n', 1)+ ids = [l for l in header_part.split('\r\n') if l.lower().startswith('message-id:')]+ assert len(ids) == 1+ assert ids[0].endswith('@email.amazonses.com>')+ assert 'folded-id' not in sent+ assert 'To: [email protected]' in header_part+ assert '[email protected]' in body++# ---------------------------------------------------------------------------# SendTemplatedEmail with SMTP relay# ---------------------------------------------------------------------------