Opened 4 weeks ago

Closed 4 weeks ago

Last modified 4 weeks ago

#36132 closed Cleanup/optimization (wontfix)

Add **kwargs to send_mail() and send_mass_mail() functions

Reported by: Nikolay Fedorov Owned by:
Component: Core (Mail) Version: 5.1
Severity: Normal Keywords: send_email, send_mass_mail, mail
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

Please add **kwargs to send_mail() and send_mass_mail() functions for pass it to EmailMultiAlternatives like extra arguments - "headers" as example.

Change History (5)

comment:1 by Tim Graham, 4 weeks ago

Description: modified (diff)
Resolution: wontfix
Status: newclosed
Summary: Add **kwargs to send_email and send_mass_mail functionsAdd **kwargs to send_mail() and send_mass_mail() functions

The send_mail() API is frozen and won't receive additional kwargs (maybe, some discussion in #20817). As the documentation says, "Not all features of the EmailMessage class are available through the send_mail() and related wrapper functions. If you wish to use advanced features, such as BCC’ed recipients, file attachments, or multi-part email, you’ll need to create EmailMessage instances directly."

Last edited 4 weeks ago by Tim Graham (previous) (diff)

comment:2 by Nikolay Fedorov, 4 weeks ago

But in user.email_user you are pass **kwargs to send_mail which doesn't support this... this is a component inconsistency.

comment:3 by Simon Charette, 4 weeks ago

The fact that user.send_mail delegates **kwargs to mail.send_mail which has a documented signature only makes it so both share the same interface.

I don't see what is incoherent about send_mail not passing arbitrary kwargs to EmailMessage. In other words, I don't see why a function that opts in **kwargs delegation should force all functions down its stack to also do so.

in reply to:  3 comment:4 by Nikolay Fedorov, 4 weeks ago

Replying to Simon Charette:

The fact that user.send_mail delegates **kwargs to mail.send_mail which has a documented signature only makes it so both share the same interface.

I don't see what is incoherent about send_mail not passing arbitrary kwargs to EmailMessage. In other words, I don't see why a function that opts in **kwargs delegation should force all functions down its stack to also do so.

I didn't mean arbitrary arguments, but I wanted to be able to use all the available documented ones for EmailMessage, such as headers. Now, for this, I have to use my own rewritten version of send_mail to simply transfer headers. This is a discrepancy between the system components and not a convenience.

comment:5 by Mike Edmunds, 4 weeks ago

If the primary request here is for the User.email_user() method to accept additional arguments like headers or reply_to, that might be a reasonable feature request. There are ways to achieve that without changing the "frozen" mail.send_mail() API, by updating User.email_user() from legacy send_mail() to using the newer EmailMessage classes. (My opinion only; I don't speak for Django.)

If that's your goal, the next step would be to raise the idea in the forum to seek feedback. It would help to include a concrete example of what you're trying to do (e.g., what headers you need to add in User.email_user()).

Or, if the primary request here is a single statement that sends mail with headers, you can achieve that now with the EmailMessage (or EmailMultiAlternatives) class:

EmailMessage(subject, message, from_email, to, headers={"In-Reply-To": "..."}).send()
Note: See TracTickets for help on using tickets.
Back to Top