[Django]-Django EmailMultiAlternatives sending to multiple "to" addresses

11👍

A list of email addresses should work, as the example in the Django docs shows.

to_emails = ['alice@example.com', 'bob@example.com']
msg = EmailMultiAlternatives(subject, text_content, from_email, to_emails)

You might want to use bcc instead of to_emails to prevent users from seeing the other recipients of the email.

msg = EmailMultiAlternatives(subject, text_content, from_email, bcc=to_emails)

0👍

I’m using a for loop. I’m also using this to filter which users emails are sent to.

for user in users:
    if user.is_active and user != request.user and user.last_login != None:
        message = EmailMultiAlternatives(
        subject = "your subject here",
            from_email = settings.EMAIL_HOST_USER,
            to = [user.email],
            )
        context = {'request':request, 'user':user, 'event':event}
        html_template = get_template("notifications/new_event_email.html").render(context)
        message.attach_alternative(html_template, "text/html")
        message.send()

It might not be the best way, but it’s working for me.

Leave a comment