Welcome back.
We want to improve our program so that instead of just sending a generic email like this, I want to
customize it to each individual.
Imagine if we have a database of all these users, all their emails as well as their first name.
Ideally, we're able to customize the email to each specific person.
So how can we do that?
Well, we're going to use an HTML based email so we can send text emails which just have text like we've
seen or we can send something more dynamic like HTML.
Now, this isn't an HTML course and it's something that we'll go into a little bit more in depth when
we talk about web development.
But in here I have a very, very basic HTML document.
And this HTML document just has a body where we want the content of our email to be.
Now in here, I wrote you just one so much money.
And then with a dollar sign name.
You can write whatever you want in here, but I want you to pay attention to the dollar sign because
we're going to do something neat here.
We're going to actually update this to reflect a variable, a Python variable that we can substitute
and customize dynamically.
Now, how are we going to do that?
Well, if we go back to our email.
I want to introduce you to two new packages built in packages in Python that we can use.
One is called the template or string template.
So from the string module, we can import.
Template.
And what this template does.
If we go to the documentation right here, we have a string dot template class and what we're allowed
to do is, as you can see here, using the dollar sign.
We can substitute variables inside of text.
You can see over here, if we zoom in a little bit, we have a template object with who likes what and
we can substitute.
These dollar signs.
RD With who?
So who equals to Tim?
We can do a template who likes what equals to us and then substitute who with Tim and what with Kung
Pao?
So this is going to be very, very useful.
And this is what we're doing with index.html.
We have a dollar sign name and ideally we're able to update this, but we have a problem here.
This is a well, it's an HTML file.
It's an index HTML file.
So how can we read this?
Convert it to a template and substitute it.
Well, we have another module that we're going to import.
And this is going to be called path lib from path.
Import path.
So this path lib and this path that we import is actually very similar to something we've seen before,
which is O's dot path.
So we could have also imported the operating system OS module and use the path method because what it
allows us to do is to access this indexed HTML file.
So this is what it's going to look like.
We can have an HTML variable that equals path.
And then give it index.html because we're in the same folder.
And this path now gives us access to index.html and we can use the read.
Text method on it to read it as a text as a string.
Now I'm going to leave a resource as to why we want to use path lib instead of Oscar path, mainly because
we have this red text method.
But there's a great article here why you should be using path lib versus the OSS path module that I'll
link to and you can read on your own.
At the end of the day, though, both of these libraries will be useful and there's no right or wrong.
Just pros and cons.
So we have this string, but now we want to update so that the name variable will be something custom.
So what we're going to do is wrap this and template.
So it becomes.
A template object.
That's it.
So we have this HTML that is a template.
And if you remember, once we have this template string, we can use the dot substitute to give it well
what we want to substitute it with.
So instead of setting content like this, instead I'm going to change this.
And say, I want a HTML dot substitute.
To equal name, which is remember the variable over here?
To have it equal.
Hmm.
Let's go with Tintin.
And by the way, this substitute can have multiple parameters.
So we can do it with keyword arguments or we can even do it as.
A dictionary like this so we can say name will be 1010 and then we can add multiple variables.
If this HTML file had multiple variables, multiple, maybe name, age, maybe a greeting, whatever
we want.
So let's leave it like that.
What's safe here and see if this works.
I'm going to send once again and this time around I want to actually send it to my Gmail account.
So we'll actually blur this out so you don't see my personal email address.
All right.
All good.
Boss, it looks like we didn't get any errors.
Now, let's check my Gmail account.
Hey, look at that.
Under important and unread.
Mind you, for you, you might get it in your spam folder.
I have an email for myself saying Hey, I just 1000.
Oh.
And the last thing we need to do when we substitute is we have to tell it, Hey, this set content is
going to be HTML.
So the way we do that is in the second parameter.
We say.
This is going to be HTML.
So it's not text like it was before, it's going to be HTML.
So parse it accordingly and set content accordingly.
So if I save this and we run.
Looks like we didn't get any errors.
That's good.
And if we go back to the Gmail client.
Hey.
Looks like I have a message that I've won $1,000,000.
That's really great.
Mind you, for yourself, you might find this in your spam folder.
So again, make sure you check your email.
Otherwise, if I click on this.
Look at that.
You just won so much money, Tintin.
How cool is that?
So just like that, we're able to programmatically send emails, customized emails.
Now there's many applications of this.
Maybe any time a customer buys a product from your company, you want to send them a personal email,
a greeting, saying, Hey, thanks for buying our product.
Or maybe you want to get yourself a reminder where every morning you send yourself an email saying,
Hey, get up and tidy your bed.
There are so many applications.
This is the cool part about Python, right?
You can do anything you want.
The world is your oyster.
So hopefully this was exciting because we have a lot more in the next video.
Bye bye.