【问题标题】:All email adresses are pasted in the To field but should go into BCC field所有电子邮件地址都粘贴在收件人字段中,但应进入密件抄送字段
【发布时间】:2016-06-15 08:34:47
【问题描述】:

我有以下使用 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 字段中的其他人。

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

【问题讨论】:

  • 我认为脚本本身没有问题;它似乎确实正确设置了标题 - (repl.it/BuZW/0) - 当块中有多个时,它会不公开(顺便说一句,如果只有 1 个,它的标题会显示电子邮件,不知道这有多可取?)也许可能是您的服务器更改了标题?不太可能,但有可能;您能否展示在收件人的邮箱中收到的标头样本?一个在 len (group)>1 块中

标签: python email smtp celery


【解决方案1】:

在这一行

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

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

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2021-06-27
    • 2014-01-21
    相关资源
    最近更新 更多