Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Beautiful confirmation emails
Lecture: Generating proper HTML content
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now, some of you may have cringed when you saw what I wrote here, and I reformatted it. So it's a little more obvious what's happening.
0:07
But look at Line 17 and go on from here. We're writing HTML in line with sort of variable replacement bits,
0:16
and then we're writing formatted text in line as well. That's not ideal, is it? How do we solve this in normal web apps?
0:24
Well, we have this whole cool section over here about, like our admin area. It uses jinja, and the jinja has this cool shared layout that has,
0:33
like, all the HTML and overall style in it. Then it has this whole that it plug stuff in that seems way nicer than just
0:42
writing in line text in our code, you can imagine we have many types of emails we want to send. Like you need to reset your password.
0:50
here's your receipt. Welcome. Things like this, right? This is the kind of stuff that would be really good if we could use jinja,
0:58
because it's exactly for building these kinds of HTML things. Check this out over here and I'm gonna paste in some code and then talk you
1:07
through it because it's kind of code you write once in an application and you never think about it again. Just works.
1:13
So look at this. I've got a little function called build_HTML Given it a jinja file and a data dictionary,
1:21
much like you would from a view method come in here. Let's import path from Pathlib. Figure out where the Templates folder is right there.
1:32
We're going to create a directory called email to go along with all of our other things and then went to use the file system loader from Jinja,
1:41
create an environment with that loader set and then render a template. You get HTML out of that couple of steps there in the basically the internals of
1:54
jinja and flask. But with this, we can now start writing HTML templates. But let's go over here and add a quick little test email.
2:08
It's a test email. We'll put a header. And what would we do, if maybe we had that order? Well, we might. Come on,
2:18
we're say, Enjoy your {{ }} double curly brace. That's how we get to strings and we say order.(dot) What do we have on
2:27
our order? We had a flavor British style, and we had similarly we had a size and that's it.
2:39
Let's Yeah, so let's just like here's some data that we could pass in and use. This is basically the certain area that we're looking for,
2:47
but we can grow it so much better. But let's try to do that again. So instead of doing this, you look, let's leave the text for a second.
2:55
Instead of doing this, you look, let's do HTML=build_HTML(), and the template file is going to be email/testemail.html.
3:10
Right. That's this and then that it's not misspelled. Take that away. We have to pass over some data, which is going to be a dictionary,
3:18
which is Here's the way the template sees it. And here's the actual data. We're providing it,
3:25
and instead of actually sending the email print would send HTML and let's just return. We don't want to just keep sending emails and cause trouble.
3:37
So let's just verify what's happening here that we're getting valid HTML. Click this button again. Didn't crash.
3:45
That's pretty good. And look what we got here would send Doc, is your test email. This is a test. Enjoy your small red velvet cake.
3:56
I guess there's a space in there, but it doesn't actually matter because HTML you know it doesn't. It's not space sensitive, like multiple spaces.
4:02
One space. It all looks the same. How awesome is that? We got it working so very cool. So here it is. We're able to very,
4:12
very easily leverage the jinja system, just like we do for HTML. Same thing for sending HTML oriented emails.