I’d like to change send_mail(), the EmailMessage constructor, and some other django.core.mail APIs to require keyword args for their more-obscure parameters. The goal is to simplify future additions/changes/removals of parameters, while allowing params to be listed in logical order in both code and docs. Seeking feedback before opening a ticket.
[This came up in reviewing the PR to add multiple EMAIL_PROVIDERS. In the updated mail APIs, the developer had originally (and reasonably, IMO) placed new provider
options alongside existing connection
params. But since that would alter documented parameter ordering, it had the potential to break existing code relying on posargs. And while I’ve suggested using kwargs-only for all new params to those functions, it would be helpful to retrofit the existing params now so we don’t run into this again later.]
In practice, I’d expect that the vast majority of code calling send_mail() is already using keywords for anything past the first four args. This proposal would make that mandatory.
Specifically, I’d like to change the current:
def send_mail(
subject, message, from_email, recipient_list,
fail_silently=False, auth_user=None, auth_password=None,
connection=None, html_message=None):
to (after a deprecation period):
def send_mail(
# these parameters can be positional or keyword:
subject, message, from_email, recipient_list,
*, # all other parameters are keyword-only:
fail_silently=False, auth_user=None, auth_password=None,
connection=None, html_message=None):
The first four params would not be affected, and would continue to allow either positional or keyword args even after the change.
Like any change to documented APIs, this would go through deprecation. During the deprecation period, posargs would continue to work for fail_silently
and on, but would issue a deprecation warning.
Similarly, send_mass_mail(), mail_admins(), mail_managers(), and get_connection() would all be changed to kwargs-only starting at their fail_silently
params. (Depending on some design decisions in the EMAIL_PROVIDERS work, we might want get_connection() to be entirely kwargs-only, but that’s a separate discussion.)
For the EmailMessage and EmailMultiAlternatives constructors, the first four subject
, body
, from_email
and to
params would continue to support both positional and keyword args, but the remaining params starting with bcc
would become keyword only after deprecation.