How to send an email to 10,000 users in Django?

There are 10,000 users in my Django app, all with emails. I would like to send an email to everyone that they say once a month. This post may contain some pdf attachments.

I tried using the EmailMessage object to send email to all of them. Before sending, I add the email addresses of all users to a composite copy of this EmailMessage.

recList = [] for recipient in rec: reci = str.strip(str(recipient)) recList.append(reci) message = (form.cleaned_data['subject'], form.cleaned_data['message'], ' emailAdmin@yahoo.com ', recList) mail = EmailMessage(form.cleaned_data['subject'], form.cleaned_data['message'], ' email_manager@mysite.org ', [' email_list@mysite.org '], 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 an email, Django complains that the “connection was reset” and does not send. I assume that the connection to the server was closed.

What is an effective way to send bulk email in Django? Will send_mass_mail() more efficient?

+6
source share
3 answers

Alternative suggestion: Sign up for the mailing list and use your APIs to support your mailing list and send out mailings. Several advantages for this approach:

  • Theyll handle any mailing requests for you, so you don’t have to worry about adding exclusion flags to your users who don’t need your emails.
  • You are less likely to receive spam filtering from your user mailboxes or annoy your hosting provider.

For example, there are API shells for MailChimp and Campaign Monitor . To add new users to the mailing list and (if necessary) delete all users who delete their accounts, it is quite easy to add to the lists.

+6
source

You must use send_mass_mail , as it will not close the connection every time. docs

I would also put messages in groups of about 100-1000, depending on how powerful your server is. The reason is that you can catch errors in small groups to try again. This also leads to a separate letter for each recipient, which is ideal. BCC's thousands of people are small.

+5
source

I think the BCC email header cannot contain 10,000 entries.

0
source

Source: https://habr.com/ru/post/904613/


All Articles