所有电子邮件地址都粘贴在“收件人”字段中,但应进入“密件抄送”字段

All email adresses are pasted in the To field but should go into BCC field

我有以下使用 python 发送邮件的脚本:

import smtplib
from celery import Celery
from email.mime.text import MIMEText
from KerbalStuff.config import _cfg, _cfgi, _cfgb

app = Celery("tasks", broker=_cfg("redis-connection"))

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in range(0, len(l), n):
        yield l[i:i+n]

@app.task
def send_mail(sender, recipients, subject, message, important=False):
    if _cfg("smtp-host") == "":
        return
    smtp = smtplib.SMTP(host=_cfg("smtp-host"), port=_cfgi("smtp-port"))
    if _cfgb("smtp-tls"):
        smtp.starttls()
    if _cfg("smtp-user") != "":
        smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
    message = MIMEText(message)
    if important:
        message['X-MC-Important'] = "true"
    message['X-MC-PreserveRecipients'] = "false"
    message['Subject'] = subject
    message['From'] = sender
    if len(recipients) > 1:
        message['Precedence'] = 'bulk'
    for group in chunks(recipients, 100):
        if len(group) > 1:
            message['To'] = "undisclosed-recipients:;"
        else:
            message['To'] = ";".join(group)
        print("Sending email from {} to {} recipients".format(sender, len(group)))
        smtp.sendmail(sender, group, message.as_string())
    smtp.quit()

当我使用脚本时,我看到邮件已发送给 group 中的每个人,但所有收件人都出现在 To 字段中,而它应该在 To 字段和 Bcc 字段中的所有其他字段。

脚本有没有错误?最初我认为这可能是我的邮件服务器的问题。

这一行

else:
    message['To'] = ";".join(group)

您正在将收件人连接到组中,并连接到电子邮件的 To 字段中,而不是 Bcc 字段中。看看这个Whosebug Post example