Since Python 3.12.1, SMTP.send_message() fails to extract 'RCPT TO' if Cc + Bcc are set · Issue #113658 · python/cpython
Bug report
Bug description:
Summary: after upgrading from Python 3.12.0 to 3.12.1, the to_addrs argument of smtplib.SMTP.send_message() is no longer automatically inferred from the email contents, as they were before and as they should according to the documentation. This seems to occur only if Cc and Bcc are both set and empty.
Reproduction:
-
Install Python 3.12.1.
-
Run a TCP server on
localhost:1025that simulates a SMTP server, e.g. with netcat:nc -v -l 1025. -
Use Python
smtplib.SMTP.send_message()to send an email:from email.mime.text import MIMEText import smtplib msg = MIMEText('Email body.') msg['From'] = 'Me <me@me.me>' msg['To'] = 'You <you@you.you>' msg['Cc'] = '' msg['Bcc'] = '' smtp = smtplib.SMTP('localhost', 1025) smtp.send_message(msg)
-
Optionally
print(msg)to see the exact bytes being sent:Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Me <me@me.me> To: You <you@you.you> Cc: Bcc: Email body. -
Receive the email and see the bug. For instance with netcat, simulate the SMTP protocol by typing
220 HELOthen220 HELOthen250 OK:Ncat: Version 7.93 ( https://nmap.org/ncat ) Ncat: Listening on :::1025 Ncat: Listening on 0.0.0.0:1025 Ncat: Connection from ::1. Ncat: Connection from ::1:42194. 220 HELO ehlo localhost.localdomain 220 HELO mail FROM:<me@me.me> 250 OK rcpt TO:<>In the data received,
rcpt TO:<>should bercpt TO:<you@you.you>.
Possibles fixes to avoid this bug:
- Downgrade to Python 3.12.0.
- Remove either of
CcorBccfrom the message. - Explicitely set
to_addrs=...inSMTP.send_message().
CPython versions tested on:
3.12
Operating systems tested on:
Linux