Opened 4 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#35864 closed Cleanup/optimization (fixed)

Warn that EmailMessage.connection option is ignored when using send_messages()

Reported by: Mike Edmunds Owned by: Mike Edmunds
Component: Core (Mail) Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mike Edmunds)

The EmailMessage connection option is ignored when using send_messages() to send multiple messages as suggested in the docs.

Here's some code that won't in fact use special handling for EU users as the author expects:

from django.core import mail

def get_notification_emails():
    notification_emails = []
    for user in get_users_needing_notification():
        email = mail.EmailMultiAlternatives(to=[user.email], ...)
        # Override the ESP for users in the EU; otherwise the default is fine.
        if user.profile.must_use_eu_data_processor():
            email.connection = mail.get_connection(host="smtp-eu.example.net")
        notification_emails.append(email)
    return notification_emails

def send_periodic_notification_emails():
    messages = get_notification_emails()
    # We replaced this loop with send_messages() to optimize sending.
    # https://docs.djangoproject.com/en/5.1/topics/email/#sending-multiple-emails
    #   for message in messages:
    #       message.send()
    connection = mail.get_connection()
    connection.send_messages(messages)

(You probably wouldn't make this mistake writing that code all at the same time, but you might stumble into it if those functions were in different files and the send_messages() optimization got added later.)

If we were designing the EmailMessage API from scratch, a clearer design would probably handle connection as an EmailMessage.send() parameter, rather than an EmailMessage property. I don't think it's worth the effort to try to change that now. (But there's an opportunity to avoid similar confusion when we introduce provider as the successor to connection in #35514.)

Suggested fix: in the EmailMessage.connection docs, note that: "This option is ignored when using send_messages()." And maybe in the send_messages() section note that it "overrides any connection option on individual messages."

We could also log a warning in smtp.EmailBackend.send_messages(), if any of the messages has a connection that is not self. (But each EmailBackend implements its own send_messages(), so this wouldn't help with third party backends.)

Change History (8)

comment:1 by Sarah Boyce, 4 weeks ago

Summary: EmailMessage.connection option ignored when using send_messages()Warn that EmailMessage.connection option is ignored when using send_messages()
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

Thank you Mike, the suggestions make sense to me

comment:2 by Mike Edmunds, 3 weeks ago

Description: modified (diff)
Owner: set to Mike Edmunds
Status: newassigned

comment:3 by Mike Edmunds, 3 weeks ago

Has patch: set

comment:4 by Sarah Boyce, 3 weeks ago

Patch needs improvement: set

comment:5 by Mike Edmunds, 3 weeks ago

Patch needs improvement: unset

comment:6 by Sarah Boyce, 3 weeks ago

Triage Stage: AcceptedReady for checkin

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 3 weeks ago

Resolution: fixed
Status: assignedclosed

In 17c8ee7:

Fixed #35864 -- Documented EmailMessage.connection is ignored when using send_messages().

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 3 weeks ago

In ffc67aac:

[5.1.x] Fixed #35864 -- Documented EmailMessage.connection is ignored when using send_messages().

Backport of 17c8ee7e3f7bf400128281b4fb283d7c209ca02b from main.

Note: See TracTickets for help on using tickets.
Back to Top