我有以下脚本用于使用 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
字段。看看这个 StackOverflow Post 示例