Skip to main content
 首页 » 编程设计

python之使用 EmailMultiAlternatives 密件抄送所有收件人

2024年10月24日11lori

我有这种发送群发电子邮件的方法:

def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None, connection=None): 
    connection = connection or get_connection(username=user, password=password, fail_silently=fail_silently) 
    messages = [] 
    for subject, text, html, from_email, recipient in datatuple: 
        message = EmailMultiAlternatives(subject, text, from_email, recipient) 
        message.attach_alternative(html, 'text/html') 
        messages.append(message) 
    return connection.send_messages(messages) 

我希望能够密件抄送所有人。也就是说,我不想在收件人中显示每个人的电子邮件,而是想隐藏每个人的电子邮件地址。

def send_mass_html_mail(datatuple, fail_silently=False, user=None, password=None, connection=None): 
    connection = connection or get_connection(username=user, password=password, fail_silently=fail_silently) 
    messages = [] 
    for subject, text, html, from_email, recipient in datatuple: 
        message = EmailMultiAlternatives(subject, text, from_email, [], bcc=recipient) 
        message.attach_alternative(html, 'text/html') 
        messages.append(message) 
    return connection.send_messages(messages) 

我做错了什么?我必须单独发送每封电子邮件吗?

编辑:

我最后只是遍历收件人列表并分别发送电子邮件。我不知道为什么它不起作用,但我不能花几个小时在这上面。它对我有用,因为该应用程序一次最多可向 25 人发送电子邮件。

我仍然不知道为什么这不起作用。 EmailMultiAlternatives 是否需要 TO 字段中的内容?我查看了 EmailMultiAlternatives 和 EmailMessage 源代码,它似乎可以在没有任何人参与的情况下发送电子邮件。我的代码有问题,但我无法确定是什么。

请您参考如下方法:

您正在使用大写 BCC。它应该是小写的。我很惊讶目前的代码此时根本无法发送任何电子邮件。

message = EmailMultiAlternatives(subject, text, from_email, [], bcc=recipient_list)