如何在 Django 中向 10,000 个用户发送电子邮件?

发布于 2024-12-23 08:04:25 字数 1572 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我纯我任性 2024-12-30 08:04:26

您应该使用 send_mass_mail 因为它不会每次都关闭连接。 docs

我还将消息分为以下组大约100-1,000,具体取决于您的服务器的强大程度。原因是您可以捕获较小组中的错误以进行重试。这也会导致每个收件人都有一封单独的电子邮件,这是理想的。密件抄送数千人并不是一件好事。

You should use send_mass_mail since it won't close the connection every time. docs

I 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.

攒眉千度 2024-12-30 08:04:26

另一种建议:注册邮件服务并使用他们的 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:

  • They’ll handle any unsubscribe requests for you, so you don’t have to worry about adding exclusion flags to your users who don’t want your emails.
  • You’re less likely to get spam-filtered out of your users’ inboxes, or to annoy your hosting provider.

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.

呆橘 2024-12-30 08:04:26

我认为,一封电子邮件 BCC 标头不能包含 10000 条记录。

I think, an E-mail BCC header cannot contain 10000 records.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文