0

I am new to Python. I have a case where I need to send email where the Bcc: list must be populated and To: list must be blank in order to hide the recipients identity.

I gave msg['TO']as '', None, [''], []. None of these worked.

I Googled, but found nothing related to my problem. I kept To: list blank in my code and some email ids in Bcc: list, then ran my code and got the following error:

    Traceback (most recent call last):
  File "C:\Users\script.py", line 453, in send_notification
    smtp.sendmail(send_from, send_to, msg.as_string())
  File "C:\Python27\lib\smtplib.py", line 742, in sendmail
    raise SMTPRecipientsRefused(senderrs)
SMTPRecipientsRefused: {'': (555, '5.5.2 Syntax error. hb1sm19555493pbd.36 - gsmtp')}

Following is my code:

msg['From'] = send_from
msg['To'] = ''
msg['Subject'] = subject
msg['BCC'] = COMMASPACE.join(send_to)
geckon
  • 8,316
  • 4
  • 35
  • 59
Krishnachandra Sharma
  • 1,332
  • 2
  • 20
  • 42

2 Answers2

1

I'm pretty sure all emails are required to have a To address. (Edit: Actually, they're not)

Whenever I receive emails that were sent to an anonymized list like what you're describing, the To and From addresses are generally both the same, which is a reasonable workaround. It looks pretty clean to the recipient:

To: some-student-org@my-university.edu
From: some-student-org@my-university.edu
Bcc: me
Community
  • 1
  • 1
nkorth
  • 1,684
  • 1
  • 12
  • 28
0

If you want to bcc using python's smtplib, don't include the bcc header, just include the bcc recipients in the call to smtplib.sendmail. For your specific example:

import smtplib
from email.mime.text import MIMEText

smtp_server = 'localhost'
send_from = 'your@email.com'
send_to = ['one@email.com', 'two@email.com']
msg_subject = 'test'
msg_text = 'hello'

msg = MIMEText(msg_text)
msg['Subject'] = msg_subject
msg['From'] = send_from
smtp = smtplib.SMTP()
smtp.connect(smtp_server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()
ToBeFrank
  • 140
  • 9