I have surfed the internet for a day but didn't find the perfect article which gives the best practices for sending mass emails.
I have configured Amazon SES using django-ses and mails are sending correctly. Now the problem is I don't know how people send mass mail, hide other recipients, which function they use, and what pattern they follow to make sending mass mail efficient and ease.
Also, we are using templates (Django Templates) for mail and below is the best solution I got by mixin all best things I found on the internet:
# 1. Getting queryset of all recipients which will receive mail
# (mine is a little bit different but at the end, it gives queryset of all emails - not list)
subscribers = EmailNotificationSubscriber.objects.all().values_list('user__user_email', flat=True)
# 2. Opening a connection
# 3. [Looping] using `.iterator()` to fetch email one by one from queryset (I think this is to handle the cases where we have an email list of around 10k or even bigger)
# 4. Creating EmailMessage instance and sending an email using `.send()`
# Function to get HTML Message (instance of `EmailMessage`)
def get_html_msg(subject, from_email, to, template_name, ctx, connection=None):
message = get_template(os.path.join(settings.BASE_DIR, 'templates', 'email', template_name)).render(ctx)
msg = EmailMessage(subject, message, from_email, to, connection=connection)
msg.content_subtype = 'html'
return msg
# Function which sends mass mail
def send_mass_mail(subject, qs, mail_template='base.html', ctx=None, fail_silently=True, *args, **kwargs):
from_email = settings.EMAIL_FROM
with get_email_connection() as connection:
for recipient in qs.iterator():
print(f"Sending to recipient: {recipient}")
msg = get_html_msg(subject, from_email, [recipient], mail_template, ctx, connection)
msg.send(fail_silently)
Above is what I am doing:
- Getting just emails and not converting it into list so I can use
.iterator() - Using a single connection to sending all mails
- Hiding other recipient using loop by sending mail one by one
(I will use the same template for all recipient so I will refactor that for performance later)
So, How people send mass mail with Amazon SES? Do they use something else over this? An open-source repo or example would greatly help.
Thanks so much
Edit 1: Removed Emojis
Edit 2: Question Narrowed