我有以下脚本用于使用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
字段中,而它应该在{{1}中显示您自己的地址}}字段以及To
字段中的所有其他字段。
脚本中是否有错误?最初我认为这可能是我邮件服务器的问题。
答案 0 :(得分:0)
在这一行
else:
message['To'] = ";".join(group)
您正在将收件人与群组联系起来,并将其连接到电子邮件的To
字段,而不是Bcc
字段。看一下这个StackOverflow Post example