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:

  1. Install Python 3.12.1.

  2. Run a TCP server on localhost:1025 that simulates a SMTP server, e.g. with netcat: nc -v -l 1025.

  3. 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)
  4. 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.
    
  5. Receive the email and see the bug. For instance with netcat, simulate the SMTP protocol by typing 220 HELO then 220 HELO then 250 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 be rcpt TO:<you@you.you>.

Possibles fixes to avoid this bug:

  • Downgrade to Python 3.12.0.
  • Remove either of Cc or Bcc from the message.
  • Explicitely set to_addrs=... in SMTP.send_message().

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs