Hi folks!
I came across an old ticket (thx Sarah B.!) which targets the developer experience (DX) of creating an email. Since the ticket has been created more than a decade ago, I’d suggest some creative freedom approaching a solution.
As some might know, I’ve taken on improving the DX for implementing emails with my class-based email approach in my package django-pony-express.
I like this approach because it’s very django-esque and easy to use. For the sake of discussion, I’ll post a quick example.
This is how a “regular” email will look like in your code:
class AccountCreatedMail(BaseEmailService):
subject = "Account created"
template_name = "account/email/account_created.html"
If you need to add context variable, you can overwrite the get_context_data()
method in this class, similar to class-based views. If you need to customize headers, translations etc, there is also a method to overwrite.
In the background there is a HTML-to-plain-text conversion happening which I do via a package. I know that this won’t be portable to Django for multiple reasons but to get started, we could just offer a plain_text
attribute and if this is not set, we just strip all the HTML tags from the HTML template and have a basic version to go with. Or we leave it out and you have to provide two templates.
Under the hood, all objects are EmailMultiAlternatives
since in 99% of all cases, you want HTML emails if you can help it.
Instead of storming forward and creating a huge PR, I’d like to gather some crowd wisdom on how to find a MVP (GVP? ) version of this which we could add to Django core.
So, please, feel free to drop ideas, criticism and feedback!
Best from Cologne
Ronny