In this lesson, we will focus on how to get the password.
So the app password in the Python program.
So what do we need to send an email to be able to actually log in and send the email.
We need the email address.
So for me that was the course Raspberry Pi email address.
And we need the app password that we have just generated before now.
The thing is you could do passwords is equal to and just write the app password here in your code.
That would work, but as best practice it's better not to put any password in the code directly.
So here we will do a very simple solution that works for simple applications.
Okay, it's not really the most secure, but it's good enough for simple stuff that you do with your
Raspberry Pi.
So instead of writing the password here, we are going to write it inside a file somewhere, and then
open the file and read the password from the file.
So let's go to the terminal.
You can open a new terminal.
Let's clear that and you go to your home directory and where are we going to put the password.
So for now if I do ls I have my temp file.
So maybe you did the same as me.
Or maybe you just copied the password in a different place.
That's really not a problem.
And in this I have my app password.
Now if I do so I'm going to do ls Dash A to see all the folders and files, including the hidden ones.
And well for example we have a dot local folder here.
So I'm going to go inside this with CD dot local.
I can press tab also and I have a share.
So I'm going to go inside the share okay.
Where I already have some stuff.
And let's say I want to put my password here and I want to name the file dot email password as an example.
So what you can do here, you can create the file with for example touch.
You create the file, then you edit it.
You can directly use nano to edit the file, but here what I will do is I will directly use this file
that we have created here.
So this temp file in our home directory.
And I'm going to move it from there to here.
How to do this.
Well I have the MV command.
And then because I'm already at the destination, I could go back to folders and then use mv temp and
then put the destination.
But I can also do this from the destination.
In this case I will do dot dot dot dot slash temp and I use the Autocompletion.
Okay, because I need to go back one folder, two folders back to the home directory.
And here is the file.
So I want to take this file and copy it right where I am.
If I want to do this I can just do dot.
Dot means the current folder.
And what I can also do is I can rename the file at the same time.
So I'm going to do dot email underscore password I press enter.
Now I have my email password.
Actually, it's a hidden file.
So I need to do a.
You see, I have my email password.
Let's just verify.
Okay.
It's here.
And if I go back to the home directory.
So yeah it's not here anymore.
Okay.
So to recap you go to dot local share and then you create a dot email password file.
And you put the password so the app password inside this.
And then we're going to read this file from the Python script.
So this method is what is better than hardcoding the password in the code for sure.
But still your password is somewhere in the Raspberry Pi okay.
It's not really protected.
So it's fine for small projects, okay.
Or if you are the only one to have access to the Raspberry Pi.
But if you have, let's say if you have a Raspberry Pi in open environment that lots of people can get
access to it, or if you want to do something a bit more professional, then you will need to use a
different way to be able to kind of connect to your Google account to send an email.
And you can use, for example, there's a protocol called OAuth two.
Okay, this is much more complicated, but it is something if you want, you can explore after the course.
For now, this solution is good.
And I would say for simple projects where you know that it's only you who have access to the Raspberry
Pi, nothing bad can happen in any way.
You have used the new Google account.
Okay, not your personal one.
So you have used the new Google account that contains no document, no photo, no files, no email.
So even if there is an issue where the worst case scenario, you don't really lose anything.
All right, now let's go to the Python code.
So I create my password.
Still I'm going to create a password variable with an empty string.
And then I will do with open okay.
So we need to open the file.
And the file is.
So let's put the absolute path slash home slash pi slash dot local slash share slash dot email password.
If you have a doubt, you can go back to the terminal and check that it's the correct path, and then
I will open it with only read permission because I'm not going to write anything, so I just need to
read from it.
And then as f or as file and then I can do password is equal to f dot read.
And that's it because the file only contains the password.
So I put what I read from the file to the password variable.
And note I created a password variable before.
As you can see this is going to be a scope issue because here we are inside a more nested scope.
So if I get the password here and then I try to use it somewhere here, then I might not be able to
get access to it.
So I first create it here.
Then I can use it inside the scope and I can continue to use it afterwards.
And just one thing is that, well, if you ever have.
So you see that we should not have any space at the end.
Here there is a newline character, I think.
But in case you are in doubt, and in case you might have added a space at the end, then the password
is going to contain a space which might produce an error later.
So you can use dots or strip after f dot read.
Okay.
So if you do this on a string because this is going to be a string, if you do a strip on a string,
it's going to remove any trailing character.
And here is going to remove the empty characters.
So the new line, the spaces, etc..
All right.
And with this we have the password that we can use in the code.
I'm not going to print it here okay.
Not going to print it with Python.
And I encourage you not to print your passwords okay.
When you run some code because well, if you have some logs that can be stored somewhere and you print
the password, then you just give the password in the logs.
And then whoever can read the log can also get your password.