如何在发送电子邮件的同时保持收件人:列表为空并且仅使用python中smtplib的sendmail函数填充Bcc:list

2024-06-16 18:16:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我把msg['TO']写成''None[''][]。这些都没用。在

我在谷歌上搜索,但没有发现任何与我的问题有关的东西。我在代码中保持To:列表为空,在Bcc:列表中保留了一些电子邮件ID,然后运行代码并得到以下错误:

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

以下是我的代码:

^{pr2}$

Tags: to代码inpysend列表电子邮件line
2条回答

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

每当我收到发送到匿名列表的电子邮件,如您所描述的,^{cd1>}和^{{cd3>}地址通常都是相同的,这是一个合理的解决办法。收件人看起来很干净:

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

如果您想bcc使用python的smtplib,不要包含bcc头,只需在对smtplib.sendmail的调用中包含bcc收件人。对于您的具体例子:

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

相关问题 更多 >