如何发送电子邮件,同时保持To:列表为空,只有Bcc:list使用python

时间:2015-08-10 12:06:37

标签: python sendmail smtplib

我是Python新手。我有一个案例,我需要发送电子邮件,其中必须填充Bcc:列表,To:列表必须为空,以隐藏收件人身份。

我将msg['TO']作为''None[''][]。这些都没有奏效。

我用Google搜索,但发现与我的问题无关。我在To:列表中的代码和一些电子邮件ID中保留Bcc:个列表空白,然后运行我的代码并出现以下错误:

    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')}

以下是我的代码:

msg['From'] = send_from
msg['To'] = ''
msg['Subject'] = subject
msg['BCC'] = COMMASPACE.join(send_to)

2 个答案:

答案 0 :(得分:1)

我很确定所有电子邮件都必须有To地址。(编辑:Actually, they're not

每当我收到发送到您所描述的匿名列表的电子邮件时,ToFrom地址通常都是相同的,这是一种合理的解决方法。收件人看起来很干净:

To: some-student-org@my-university.edu
From: some-student-org@my-university.edu
Bcc: me

答案 1 :(得分:0)

如果您想bcc使用python' smtplib,请不要包含bcc标题,只需在通话中加入bcc收件人到smtplib.sendmail。对于您的具体示例:

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