{"id": 36163, "type": "Cleanup/optimization", "summary": "Change mail APIs to (mostly) keyword-only parameters", "description": "Several of Django's email APIs have extremely long parameter lists. This complicates adding\u2014and documenting\u2014new parameters where they would logically fit, as the existing positional order must be maintained for compatibility.\r\n\r\nThis ticket proposes changing the public django.core.mail APIs to require keyword-only arguments, starting at the first boolean or \"uncommon\" parameter. That's `fail_silently` for the functional APIs, and `bcc` for the class constructors (specifics below).\r\n\r\nForum discussion: https://forum.djangoproject.com/t/change-send-mail-and-emailmessage-to-kwargs-only/38239\r\n\r\n~~Consensus in the forum was that this change should be made '''without a deprecation period.''' Existing code that uses positional arguments for the affected params would raise a TypeError after the change.~~ __During a standard deprecation period, the affected parameters would be accepted as either positional or keyword arguments, but using posargs would raise a deprecation warning. After the deprecation period, continuing to use posargs for those params would result in Python's usual TypeError.__ [Edit: further forum discussion clarified need to follow the deprecation process.]\r\n\r\nA quick (and by no means exhaustive) GitHub code search suggests a lot of existing code already uses keyword arguments for these params, but that positional arguments are common for the earlier ones. One potential exception is custom EmailMultiAlternatives subclasses. (More details in the forum.)\r\n\r\n(This originally came up in the context of #35514, which must add a new `provider` parameter to several existing APIs\u2014including the \"[https://github.com/django/django/blob/8eca4077f60fa0705ecfd9437c9ceaeef7a3808b/django/core/mail/__init__.py#L78-L79 frozen]\" send_mail() and send_mass_mail() functions, and the EmailMessage class with its [https://docs.djangoproject.com/en/5.1/topics/email/#emailmessage-objects:~:text=in%20the%20given%20order%2C%20if%20positional%20arguments%20are%20used documented parameter ordering].)\r\n\r\n=== Proposed changes ===\r\n\r\nThis change would add `*,`s where indicated below. All parameters after the `*,` would become keyword only.\r\n\r\n{{{#!python\r\n# django/core/mail/__init__.py\r\ndef get_connection(\r\n    backend=None, \r\n    *, \r\n    fail_silently=False, **kwds):\r\n\r\ndef send_mail(\r\n    subject, message, from_email, recipient_list,\r\n    *,\r\n    fail_silently=False, auth_user=None, auth_password=None,\r\n    connection=None, html_message=None):\r\n\r\ndef send_mass_mail(\r\n    datatuple, \r\n    *,\r\n    fail_silently=False, auth_user=None, auth_password=None,\r\n    connection=None):\r\n\r\ndef mail_admins(\r\n    subject, message, \r\n    *,\r\n    fail_silently=False, connection=None, html_message=None):\r\n\r\ndef mail_managers(\r\n    subject, message,\r\n    *,\r\n    fail_silently=False, connection=None, html_message=None):\r\n}}}\r\n\r\n{{{#!python\r\n# django/core/mail/message.py\r\nclass EmailMessage:\r\n    def __init__(\r\n        self,\r\n        subject=\"\", body=\"\", from_email=None, to=None,\r\n        *,\r\n        bcc=None, connection=None,\r\n        attachments=None, headers=None,\r\n        cc=None, reply_to=None):\r\n\r\n\r\nclass EmailMultiAlternatives(EmailMessage):\r\n    def __init__(\r\n        self,\r\n        subject=\"\", body=\"\", from_email=None, to=None,\r\n        *,\r\n        bcc=None, connection=None,\r\n        attachments=None, headers=None,\r\n        alternatives=None,\r\n        cc=None, reply_to=None):\r\n\r\n}}}\r\n", "severity": "Normal", "status": "closed", "resolution": "fixed", "custom": {"easy": "0", "stage": "Ready for checkin", "ui_ux": "0", "has_patch": "1", "needs_docs": "0", "needs_tests": "0", "needs_better_patch": "0"}}