Skip to content

Send queued mail through MAILERS on Django 6.1 - #191

Open
phedoreanu wants to merge 1 commit into
pinax:masterfrom
phedoreanu:mailers-setting
Open

phedoreanu wants to merge 1 commit into
pinax:masterfrom
phedoreanu:mailers-setting

Conversation

@phedoreanu

Copy link
Copy Markdown

Django 6.1 deprecates the EMAIL_* settings in favour of MAILERS. As soon as a project defines MAILERS, send_all() stops working:

  • it reads settings.EMAIL_BACKEND, which raises AttributeError when MAILERS is defined
  • it calls get_connection(backend=...), which raises RuntimeError: get_connection(backend, ...) is not supported with MAILERS.

So a project on 6.1 has to keep the deprecated settings to keep its queue moving.

This follows the upgrade path for wrapper backends from DEP 18 that #190 points to. DbBackend takes a using option naming the mailer that does the real sending:

MAILERS = {
    "default": {
        "BACKEND": "mailer.backend.DbBackend",
        "OPTIONS": {"using": "smtp"},
    },
    "smtp": {
        "BACKEND": "django.core.mail.backends.smtp.EmailBackend",
        "OPTIONS": {"host": "smtp.example.com"},
    },
}

send_all() reads using from the settings and sends through mailers[using]. A few decisions worth a look:

  • The queue doesn't record which mailer a message came through, so several DbBackend mailers must share one using. Different values raise ImproperlyConfigured. Supporting them properly would need a column on Message, which I left out.
  • With MAILERS defined but no DbBackend mailer (mail queued with mailer.send_mail() and friends), the default mailer delivers.
  • using pointing at another DbBackend mailer raises, like the existing EMAIL_BACKEND/MAILER_EMAIL_BACKEND loop check.
  • MAILER_EMAIL_BACKEND is ignored when MAILERS is defined. Without MAILERS nothing changes, on any Django version.

send_all() also stops assigning email.connection, which 6.1 deprecates, and calls connection.send_messages([email]) instead. EmailMessage.send() used to skip a message with no recipients, which is what makes the don't-send list work, so that check is kept explicitly (test_blacklisted_emails covers it).

Tested with pytest on Django 6.1.3 and 5.2.13 (Python 3.14). The new tests are skipped below 6.1. I didn't add a 6.1 env to tox or the workflow; happy to if you want it in this PR.

Refs #190

Django 6.1 deprecates the EMAIL_* settings in favour of MAILERS. Once a
project defines MAILERS, send_all() broke in two places: it read
settings.EMAIL_BACKEND, which is no longer available, and it called
get_connection(backend=...), which Django rejects with MAILERS.

DbBackend now takes a "using" option that names the mailer which does
the real sending, as DEP 18 recommends for wrapper backends. send_all()
reads it from the settings and sends through mailers[using]. Without a
DbBackend mailer the default mailer delivers. Projects that still use
EMAIL_BACKEND and MAILER_EMAIL_BACKEND behave as before.

The message is now handed to the connection with send_messages()
rather than by setting email.connection, which is deprecated too. A
message whose recipients are all on the don't-send list is still
skipped, as EmailMessage.send() did.

Refs pinax#190
@gaeldb

gaeldb commented Nov 26, 2026

Copy link
Copy Markdown

Thx!
Can someone please review and merge?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants