How does one send an email to 10,000 users in Django?
Asked Answered
O

3

10

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?

Oliviero answered 28/12, 2011 at 17:23 Comment(2)
Note you forgot to check the recipient.hasSignedUpForEmail flag. Also, less humorously, why do you create 'message' and why is it inside the loop, getting overwritten each time?Keefer
have you tried through code.google.com/p/django-mailerMajesty
P
8

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.

Parthenon answered 28/12, 2011 at 18:0 Comment(0)
U
9

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.

Ululant answered 28/12, 2011 at 17:43 Comment(0)
P
8

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.

Parthenon answered 28/12, 2011 at 18:0 Comment(0)
F
0

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

Fontana answered 28/12, 2011 at 17:35 Comment(1)
This is correct. It depends on the email service, but Amazon SES for example allows a maximum of 50 recipients in BCC.Mortimer

© 2022 - 2024 — McMap. All rights reserved.