如何在 Django 中向 10,000 个用户发送电子邮件?
我的 Django 应用程序有 10,000 个用户,全部都有电子邮件。我想给他们所有人发一封电子邮件,比如每月一次。此消息可能包含一些 pdf 附件。
我尝试过使用 EmailMessage 对象向所有人发送电子邮件。在发送之前,我将所有用户的电子邮件地址添加到此 EmailMessage 的密件抄送组件中。
recList = []
for recipient in rec:
reci = str.strip(str(recipient))
recList.append(reci)
message = (form.cleaned_data['subject'], form.cleaned_data['message'], '[email protected]', recList)
mail = EmailMessage(form.cleaned_data['subject'], form.cleaned_data['message'], '[email protected]', ['[email protected]'], recList)
num_attachments = 0
if form.cleaned_data['attachment'] != None:
email_attachment = EmailAttachment(
document_name = form.cleaned_data['attachment'].name,
email_message = email,
document = form.cleaned_data['attachment'],
)
email_attachment.save()
mail.attach_file(settings.MEDIA_ROOT + "/" + email_attachment.document.name)
mail.send(fail_silently=False)
然而,当我发送电子邮件时,Django 抱怨“连接已重置”并且不发送。我假设服务器连接已关闭。
在 Django 中发送大量电子邮件的有效方法是什么? send_mass_mail()
会更有效吗?
My Django application has 10,000 users, all with emails. I would like to send an email message to all of them say once a month. This message could have some pdf attachments.
What I have tried is using an EmailMessage object to send an email to all of them. I add all users' email addresses to the bcc component of this EmailMessage before sending.
recList = []
for recipient in rec:
reci = str.strip(str(recipient))
recList.append(reci)
message = (form.cleaned_data['subject'], form.cleaned_data['message'], '[email protected]', recList)
mail = EmailMessage(form.cleaned_data['subject'], form.cleaned_data['message'], '[email protected]', ['[email protected]'], recList)
num_attachments = 0
if form.cleaned_data['attachment'] != None:
email_attachment = EmailAttachment(
document_name = form.cleaned_data['attachment'].name,
email_message = email,
document = form.cleaned_data['attachment'],
)
email_attachment.save()
mail.attach_file(settings.MEDIA_ROOT + "/" + email_attachment.document.name)
mail.send(fail_silently=False)
However, when I send the email, Django complains that "The connection was reset" and does not send. I am assuming that the server connection was closed.
What's an efficient way to send a mass email blast in Django? Would send_mass_mail()
be more effective?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用
send_mass_mail
因为它不会每次都关闭连接。 docs我还将消息分为以下组大约100-1,000,具体取决于您的服务器的强大程度。原因是您可以捕获较小组中的错误以进行重试。这也会导致每个收件人都有一封单独的电子邮件,这是理想的。密件抄送数千人并不是一件好事。
You should use
send_mass_mail
since it won't close the connection every time. docsI would also chunk the messages into groups of about 100-1,000, depending on how powerful your server is. The reason is that you can catch errors in smaller groups for retrying. This also results in a separate email per recipient, which is ideal. BCC'ing thousands of people is not great.
另一种建议:注册邮件服务并使用他们的 API 来维护您的电子邮件列表并发送邮件。这种方法有几个优点:
有一些 API 包装器可用于 MailChimp 和 广告活动监控。添加挂钩以将新用户添加到邮件列表并(如果相关)删除任何删除其帐户的用户应该相当容易。
An alternative suggestion: sign up to a mailing service and use their APIs to maintain your email list and send out mailings. A couple of advantages to this approach:
There are API wrappers available for, among others, MailChimp and Campaign Monitor. It should be fairly easy to add in hooks to add new users to the mailing list and (if relevant) remove any users who delete their accounts.
我认为,一封电子邮件 BCC 标头不能包含 10000 条记录。
I think, an E-mail BCC header cannot contain 10000 records.