Back to the video

Tying up a loose end - How class-based emails will save your day

Transcript

By Speaker 1, Speaker 2. From DjangoCon Europe 2024. Published July 11, 2024.

Watch the video Markdown version 3,175 words · auto-generated · en

Summary

Ronny Vedrilla presents Django Pony Express, a class-based email service intended to remove repetitive and error-prone work from sending emails in Django applications. A project-specific base email class can centralize templates, context such as logos and unsubscribe links, headers, reply-to handling, subject prefixes, validation, error handling, logging, attachments, and HTML-to-plain-text conversion, while individual emails require only a small amount of code. The package also provides optional threaded sending, factories, and an email test service that makes it easier to query Django’s test outbox and verify recipients, subjects, headers, and content in both HTML and plain-text parts. He argues that the abstraction remains useful even for developers who dislike class-based views, because it packages functionality that would otherwise be repeatedly implemented, and that the open-source code can be maintained or copied into a project if necessary.

Transcript

Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.

0:00 Introduction and Email Motivation

Speaker 1

Okay, give him a big hand everybody. Ronnie.

Speaker 2

Hi. Okay. Yeah, hi, my name's Ronnie and today I want to go to the first one. I want to talk about a pack I've built in the last year, last two years, and it's called Django Pony Express and I hope I can convince you why it's a good idea to use it. Well, this is the agenda. I'm going to do a brief introduction try to motivate why this email thing is important to me. Then we have an extensive history lesson, just kidding. Then I talk about the status quo in Django then I present a solution. Okay, that's me in front of a castle. I like history stuff. And

I study business information systems. I've been working with Django since twenty twelve, which is version one point four, the older might remember. I'm an individual member of the Django Software Foundation and I host the Django Meetup Cologne, which is one of the few Django meetups to survive COVID, which is really sad. And yeah if you want to Well communicate with me. LinkedIn is the only social media I have, so find me. I think I'm the only person with that name there. I work for a company called Ambient. We're a full service agency, we do web and apps and If we can choose, we use Django and they give me the opportunity to be here, so I'm very thankful for them to them. So, emails.

1:40 Email Development Pain Points

Speaker 2

This is a question that I thought a lot of times and I've heard from other people working with Django. It's not so much fun working with emails, especially if you build larger applications, which we basically do. Business applications, trying to build small ELP systems, and you will have usually many emails there. Especially if you have n customers on your email then on your platform then you will need even more emails. And sure somebody might say yeah that but you know in ept notifications or things like this. But in the end how do you inform your users that you have in-ept notifications? Well, you send an email, so back to square one. So yeah, I guess we can agree that emails are an integral part of most Django applications, especially business applications.

I think in the talk from J Yesterday about the background workers we also saw stuff about emails. So yeah. I've been working with maybe twenty-five business applications, built twenty-five business applications in my years And these are the biggest pain points. At first that you have the not dry HTML part and plaintext part, which is kind of annoying and very un-jango-esque that you have to worry about this yourself. Then usually emails would have like a Template within your CI with some logos, some unsubscribed links, so on. And you would need variables for this. And you would have to add them in every single email. And again, it's not dry. I usually tend to forget it.

And it's tedious. Mm then working with emails kind of feels like it is an external API. Spoiler, it is an external API, at least the dispatching. But for example they won't really unit test it because it's annoying, it's possible, but you know If it's not convenient, probably you won't do it. And like in one project maybe I would start adding convenience features, like you know, some headers or stuff like this, an unsubscribe link, like a good pattern for this. And in the next project I would have to write myself, oh yeah I already did this and copied over and this is not the way I like to work. So I set the package called Django Pony Express and we all know the great Django Pony here. And the Pony

3:59 Django’s Email Status Quo

Speaker 2

Express was an Mailing service, like an analog mailing service in the eighteen sixties in the US. It was super fast. Unfortunately it was superseded only one year later by the telegraph. Nevertheless, it's about mailing and it's about ponies, so I thought the name is very fitting. All right. So if you look at the Django documentation and you if you look up emailing, then you basically get this. It works, it's fine, but it's very simple, everything is set directly, there are no variables, there's no abstraction, and usually it doesn't Really match the emails I have to build because they more look like this abomination. I can see that you can't read any of it, that's not a problem, because that's kind of the part. Because what you what I have to do basically for every email, you have to set the

from email, you have to set the two email, maybe even fetch the two from some user object or something. There would be internationalization. There would be headers to set, there would be the two templates I would render for the HTML part, for the plaintext part. There would be error handling. would be logging, there would be actual sending of an email and in case it's an HTML email and usually this is a thing what we want to do, would be an email multi alternatives class, which is not so intuitive, I would say. So yeah, lots of stuff going on. I always had a function, so I would basically copy it over and tend to forget things and again it's not very convenient. So this means a lot of redundant code. It's very easy to make mistakes, trust me.

5:30 Class-Based Email Services

Speaker 2

We have the duplicate HTML plaintext part. It's the not so it the felt not so encapsulated email API from Django. You can't really inherit anything, and every log message would look a little bit different, maybe if we email would look a tiny bit different because I would build it per on a per email basis. So if you want to do some logging aggregation or something, it would be very tedious to actually find okay, this is all the email stuff. Okay, so My solution or our solution is the Pony Express. And as you have maybe guessed from the name of the talk, it's class based. So after introducing the concept, many of my colleagues said yeah that's actually quite neat.

So what does this mean? Probably you all know the class based views in comparison to the function based views, and the idea is that you have kind of an abstraction which does the regular case. For you. And if you need something, then you can override specific methods and customize what you need to customize. But if you don't want to customize anything, you're just good to go with very few lines of code. So there's a base email service class and if you extend from this, then you basically get all the features I've shown in this very huge block of code before, basically included in this. You don't have to worry about this anymore. It works out of the box, it's very easy to stay dry, and obviously docs are. didn't happen. Everything is documented

with the docs. This QR code will guide you to Pypei if you're interested, but you can also Google for it. Yeah, so class-based emails. This is an example. Probably maybe I don't know if you can read it. The thing is, it's a very small thing and it's just you extend from a class. You have the subject there, you define And that's it basically. You don't have to worry about code duplication, don't have to worry about how to actually send the email or actively think about do I want an email multipath alternative or not. You don't have to have duplicated templates anymore. You can if you want, but usually you don't care.

7:49 Project Base Classes and Async Support

Speaker 2

It's weaeless code, it's testable. This is I think the key feature, but I'll come to this in a second. It's very fast to create new emails, it's neat, we're nerds, so we not like neat things. And yeah, I'm not sure if you can read it, but here, as I said in the slide before, I talked about base email service. This is a custom base email service, and this is basically how you adapt the this generic package to your project. So when you when you start a new project and you install the PonyExpress, what you need to do is you create a new email base class specific for your project. This means you would just extend from the base email service. And then for example you can set in the getContext data method you might look familiar to you from the views.

Then you can just define all the all the variables you need for your base template. For example, the unsubscribed link or you know where your logo lives or something like that. To have a little bit new content for the people who have seen me last year at the Django Day in Copenhagen, which was really great, I added a tiny bit of async support because you shouldn't send emails in the main thread and I tried to basically find a solution that you don't have to think about anything anymore. So by default the Pony Express would send emails in the main thread, which is not good. So I started new base classes. It would be a threat email service. It's very easy. I'm not really sure if this is the best thing for extensive production use, but if you take the pattern it's very easy to create something for

9:25 Pony Express Features

Speaker 2

Salary, Django q or maybe even the background workers. So I' very eager to see where this goes and if they come in I promise I will add the functionality to this. Yes so here are some features which are included in the in the Express. It's really sad that it's really impossible to see anything from the code, but yeah, well, happens. If you want, you can customize it by overwriting the get translation method, quite obvious. Then there's another thing I would I named the reply to

dilemma, is that usually a you send your emails from no reply at your domain. But sometimes people want to reply to this because maybe there's an error, a bug or they think hey why do I get this email? I didn't I didn't sign up for it. So what you can What you can do is you can set the reply to address header. And if somebody replies to this, then it's the it the email client automatically takes this address instead of the sender, which is kind of neat. And you still can communicate, hey, this email was Sent by a human if you look at the no reply. And yeah, you can just set this in your base class and then it will be automatically set. You don't have to worry about any of the of the details of how to actually do it.

Yeah, some additional candy. There's an under the hood HTML text conversion, so you don't need the HTML the plain text email, the plain text template, sorry. And yeah if you want to override this, if you care about the plain text for example, if for example Some email clients use the plain text as a preview if it pops up and if you really care about that everything looked great then you shouldn't use this because it kind of makes some odd things. But if you don't care and just want it to work in case somebody, you know, has disabled HTML emers, you can just use it. There is a subject prefix which helps you to for example you have a service called my project, whatever. Then you can prefix all your emails, for example, account created, new order received.

Account deleted, you can prefix with your for example my project thing. So if the customer it has a similar look and feel for all your emails, the customer needs email client can you search for all the emails, group the emails, etc. You can add some custom validation in your in your custom email class if you need it, for example to check hey, only active users should get this email or add some email specific business logic if you want. There 's Some error handling you can c you can customize and I have factories. These factories imagine for example you want to send all to all your managers some monthly report, but they all have the email itself is the same but The content is a tiny bit different.

Then you can use the factories. It's a concept that's in Django. It's a little bit difficult and it's kind of an edge case, I would feel. But nevertheless, it's in there so you can use it. There would be some basic logging. And I added a switch there because sometimes in logging it's quite imp quite important to know who didn't get the email if you know there's an error or something. But obviously you don't want to leak personal data into some logging service. So privacy by design, it's disabled, but if you know what You're doing you can enable it so it will log by default the email address and the error, obviously. Yeah, and you can use attachments, but this is not something you would expect, I would guess. Okay. So this I think is the

13:17 Email Testing Utilities

Speaker 2

USB feature of this. It's not really connected to the to the class based emails and it could be a standalone package. I actually thought about if I should split this out, but for now I think it's It's valid that it lives inside this package. And yeah, what happens if you want to test emails in your unit test? When your code triggers c when your code triggers an email, then the Django test framework if you use Django test framework, it will add this to the to a mail dot outbox list in the end. And if you want to get the HTML part of the first email in this outbox, you would need to do like Mail. outbox on position zero.

alternatives on position zero on position zero and then you can access the HTML part. It's a little bit clunky, right? So what there is now an email test service and this test service is In practice just a wrapper for this list. But there are lots of methods which makes you interact with it like a Django query set. You can filter by subject, you can filter by recipient, you can filter by some other headers, right? count how many emails are in there. And a very typical example would be that you say, okay, I want to test to see if you know my code sends the correct emails. So you check how many emails are in the in the outbox. So you use this email test service and you count it.

Then you trigger your code to see, okay, I want to trigger the email. Then you check okay, is there one email or maybe five, depending on your use case, more in the outbox. Okay, great. Then you check is the subject correct? And Then you have to check obviously the content, the recipient, maybe the from, depends on what you're doing. And the most important part obviously is the content. Again, even though you don't have to set explicitly an plaintext template, it's still there because it's part of the email protocol how it works. So there's a helper. It's called a third body contains and it will check for you in both parts so you don't forget, you know, to update one part. This is in this happens a lot in reality. So Yeah. There's even more stuff this thing can do.

15:34 Async Roadmap

Speaker 2

Just have a look at the documentation if you're interested, or ask me. And yeah, Outlook. As I said, this talk about the background workers yesterday was very inspiring and I'm really hoping to see them soon. And yeah, I think this would be a very, very neat addition to solve this async problem. Maybe provide some additional async base classes for salary Django Q and so on. I think I will go by demand on this one. And yeah maybe write a depth to get this thing in Django Cork.

16:25 Critical Review and Project Outlook

Speaker 2

Okay, so critical review. There's There are lots of voices against class based views. So some people say, Okay, this all looks great, but why should we put our money on class based emails if class based views somehow are maybe discouraged? Well, in the end I built lots of things which are not there. So if you don't use the package, then you would have to build it by yourself. And there is no function-based alternative, like this convenient. So I would say it's still worth it, even if you're no fan of cloud-based use. Then And some people would say, Hey, you know, I'm you know, have hundreds of emails in my project and I built them all the Pony Express. What if it's gets discontinued at some point? Well as always you can always connect on GitHub, it's open source

obviously. You can always w write to me, I'm the maintainer. And in the worst case that you know, everybody is was hit by a bus and nobody's there, you can still just copy the code over in your project and maintain it from there. It's still easier than writing everything by yourself, trust me. Okay, and last but not least, if this really ties up a loose end in Django, shouldn't Django provide a way to deal with this? Maybe, but it doesn't. So I do, thank you.