Skip to content

smtplib not honoring bcc header #75235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zoof mannequin opened this issue Jul 26, 2017 · 2 comments
Closed

smtplib not honoring bcc header #75235

zoof mannequin opened this issue Jul 26, 2017 · 2 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@zoof
Copy link
Mannequin

zoof mannequin commented Jul 26, 2017

BPO 31052
Nosy @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-07-27.04:34:53.453>
created_at = <Date 2017-07-26.23:43:32.658>
labels = ['invalid', 'type-bug', 'library']
title = 'smtplib not honoring bcc header'
updated_at = <Date 2017-07-27.04:34:53.446>
user = 'https://bugs.python.org/zoof'

bugs.python.org fields:

activity = <Date 2017-07-27.04:34:53.446>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2017-07-27.04:34:53.453>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2017-07-26.23:43:32.658>
creator = 'zoof'
dependencies = []
files = []
hgrepos = []
issue_num = 31052
keywords = []
message_count = 2.0
messages = ['299278', '299293']
nosy_count = 2.0
nosy_names = ['r.david.murray', 'zoof']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue31052'
versions = ['Python 2.7']

@zoof
Copy link
Mannequin Author

zoof mannequin commented Jul 26, 2017

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()

@zoof zoof mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 26, 2017
@bitdancer
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant