Search code examples
pythondjangoemailamazon-sesdjango-ses

How to send promotional/mass mail in Django with Amazon SES and hide other recipients


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:

  1. Getting just emails and not converting it into list so I can use .iterator()
  2. Using a single connection to sending all mails
  3. 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


Solution

  • according to Your questions i think that firstly, when sending mass mail, You should take into account the reputation of Your SES account which can drop down massively if You decide to buy a recipients email list. In order to achive that, it is good to use Dedicated IP's in SES. Here is link to that: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/dedicated-ip.html

    According to Your questions:

    1. The best way would be to include them in bcc header, as You have set, the limit can depends on SMTP server. According to the AWS, it is 50 recipient's per message. So what You can do is You can group all of the mails and make one call with 50 recipients so it would lower Your request amount significantly.

    2. Mass mailing has two approaches. One that, You have already tried(so one by one, and the other one which is considered better -> sending in bulks, as described above). If You would like to check something about for instance bulk sending You can see it there: https://aws.amazon.com/blogs/messaging-and-targeting/introducing-email-templates-and-bulk-sending/ Also, it is good to use only BCC field. You are ensuring Yourself, that there will not be any mistakes in displaying to the recipients email addresses of the other users.

    3. Check that links, that I have already provided.

    4. Yes, it is. You can easily use amazon ses templates, or create Your own ones. Also, the customization, managing Your own domains, configuration sets is really helpful and the documentation is quite good. However there are small drawbacks, and some minor bugs(for instance when You verify the domain, and it doesn't work instantly, there is a possibility that removing and adding it one more time fixes it :) ) However You have to be careful about Your reputation on SES.

    5. Practises:

    • Don't use TO/CC, instead use BCC
    • Always monitor Your sending reputation, unless You want Your account to be blocked
    • Invest in dedicated ips
    • Block emails, which had already bounced before(Amazon has it's own suppression list, however it is much better to have one more on Your side)
    • Be aware of the sending quota, that You already have assigned and extend it for Your purposes
    • Don't buy email list
    • You can verify recipient's emails address existence before email sending
    • Be aware of the compliaints
    • Don't use link shorteners