【发布时间】: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块中