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