Opened 7 minutes ago
Last modified 5 minutes ago
#37540 assigned Bug
send_mail() can ignore fail_silently=False with a legacy EMAIL_BACKEND
| Reported by: | Cleisson | Owned by: | Cleisson |
|---|---|---|---|
| Component: | Core (Mail) | Version: | 6.1 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
On Django 6.1, send_mail() can return zero instead of raising a delivery error when a project still uses EMAIL_BACKEND. This happens with a custom backend whose constructor defaults to fail_silently=True. Both omitting the argument to send_mail() and passing fail_silently=False explicitly should let the exception reach the caller.
Save this as mail_repro.py and run python mail_repro.py with Django installed. The backend simulates a failed delivery without contacting a mail server.
from django.conf import settings from django.core.mail import send_mail from django.core.mail.backends.base import BaseEmailBackend settings.configure(EMAIL_BACKEND="__main__.Backend") class Backend(BaseEmailBackend): def __init__(self, *, fail_silently=True, **kwargs): super().__init__(**kwargs) self.fail_silently = fail_silently def send_messages(self, messages): if self.fail_silently: return 0 raise OSError("connection refused") for options in ({}, {"fail_silently": False}, {"fail_silently": True}): try: result = send_mail( "Subject", "Body", "a@example.com", ["b@example.com"], **options ) except OSError as error: result = f"OSError: {error}" print(options, result)
On Django 6.0.10 the first two calls raise the simulated error.
{} OSError: connection refused
{'fail_silently': False} OSError: connection refused
{'fail_silently': True} 0
On 6.1, 6.1.3, and main at f0e7bd9088, all three calls return zero. An application that catches delivery exceptions can miss the failure.
This started with 0f581cd29d (#35514). With EMAIL_BACKEND set and MAILERS unset, get_connection() now passes the flag to the backend constructor only when it is true. Omitting the keyword leaves this backend's own default in effect. In #36894 the caller supplies an existing connection. This example lets Django construct the backend.
A patch and regression test are on my fork. With the patch, the script gives the 6.0.10 output again.
OpenAI Codex (GPT-6.2 Astra and Sol) helped investigate the regression, prepare the patch and tests and run the example.
Here’s the patch mentioned in the report: https://github.com/cleissonom/django/pull/5