This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: smtplib not honoring bcc header
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: r.david.murray, zoof
Priority: normal Keywords:

Created on 2017-07-26 23:43 by zoof, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg299278 - (view) Author: zoof (zoof) Date: 2017-07-26 23:43
When I try sending an email, using smtplib, with the bcc header set, the bcc header is included in messages send to the "to" and "cc" addresses. According to section 4.5.3 of rfc 822:

    > The contents of this field are not included in copies of the message sent to the primary and secondary  recipients.

So this behavior is incorrect. It should not be up to the mail client to ignore the bcc field.

Here's a script that can replicate the problem:

#!/usr/bin/env python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

body = "this is a test"

#craft the message
fromaddr = 'ned@example.com'
server = smtplib.SMTP('smtp.example.com', 587)
p = 'Hunter2!'
subject = "test"
toaddr = "foo@example.com"
ccaddr = "bar@example.com"
bccaddr = "baz@example.com"

msg = MIMEMultipart()

msg['cc'] = ccaddr
msg['bcc'] = bccaddr
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
 
msg.attach(MIMEText(body, 'plain'))

#send the message
server.starttls()
server.login(fromaddr, p)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
msg299293 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-07-27 04:34
smtplib in 2.7 doesn't know anything about RFC822 or any of the replacement RFCs.  sendmail accepts a *string*, and doesn't understand or modify anything about that string except the newlines.  It is your responsibility not to *add* the BCC header.  What you want to do is put the BCC (and CC!) recipients in your toaddr list passed to sendmail, and *not* add a BCC header.

In Python3 smtplib has a send_message method that accepts a Message object, and that method uses the BCC to inform where to send the message and strips the header before sending. That is, smtplib's send_message method *does* implement RFC5322 behaviors.
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75235
2017-07-27 04:34:53r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg299293

resolution: not a bug
stage: resolved
2017-07-26 23:43:32zoofcreate