This is step number four for this final project.
And in this step we are going to add the email functionality in our program.
So we already take a photo.
After we detect a movement for more than five seconds.
We also save the photo file names inside the log file.
And now we're going to send also that photo by email.
So let's start and let's initialize an email client.
So with Gmail I'm going to import Gmail here for example here import Gmail.
Then where do I set up my email for example here.
Well you could do it wherever you want.
I'm just going to do it here just after I create the global variables and to set up Gmail I will need
the email address and also the password.
And this is the password I have already saved.
So in the previous lesson in the section on emails.
So I'm first going to get the password from the file where I saved it.
Let's add a comment here.
Setup email and then password.
So that's an empty string.
And then I do.
We've open and I have saved the password in home slash pi slash dot local slash share.
Slash dot email password.
All right.
That's where I saved it.
I'm going to open this with only read permission because I don't need more than that.
And then as f for file and the password is going to be f dot read.
And I'm also going to add dot a strip.
Okay.
Just to remove any unwanted space at the end or stuff like that.
So we are sure that we only have the password.
Then I can create my email client.
Let's just name it YAG as we did before is equal to YAG mail dot SMTP uppercase.
And that's going to be the email address you used.
So the one you created as the sender of the email.
Okay.
So formula was course dot Raspberry Pi at gmail.com for you is going to be different.
And then the password.
Once we are done with this I can also do print email setup okay.
All right.
So the email is set up.
Then what we're going to do is we're going to after here we take a photo and we update the photo log
file.
We are also going to send that photo by email.
So let's create a function as we did before.
Here I'm going to create a third function def send a photo by email for example.
And I will pass as an argument the email.
Uh, client.
I could name it my client as I want.
And also the file name.
Okay.
For the photo.
And here, once again, I don't use the global variable directly.
I'm going to pass the variable when I call the function.
So then I get it as a parameter here.
So I could basically export this function and use it anywhere else.
And I will do Gmail client dot send.
I will need to put two.
So that's going to be your uh your email address.
I'm going to change it later.
But for now it's going to be your email address, the one you want to receive the email to.
Okay.
So that one here is a recap.
This one is the sender.
And this one here is the receiver.
And then I put a comma I go to a new line.
Subject is going to be let's say new photo from your from Raspberry Pi.
You can write whatever you want.
I'm gonna also add contents with an S, which is just.
For example, check out the new photo.
And then the most important thing attachment is equal to.
And we directly have the file name.
And I close the parentheses.
All right.
So we directly send the attachments that corresponds to the photo we have taken.
So let's go to here.
When we take the photo and we update the log file I'm going to then call send photo by email I'm going
to provide a YAG.
YAG is the YAG mail client I have created right here.
Okay.
Then I'm going to pass as the first argument here and then the file name.
What is the file name?
Well, this is the file name that I got right here.
Photo file name.
So photo file name.
Finding.
All right, so you see, what we do here is we call three functions one by one.
And sometimes we even like here we even use the result of one function inside the following functions.
I'm going to okay.
This time I'm not going to save it as step three I'm going to do file save as step four okay.
And before we run this let's do a very quick recap because that's going to be the end of that program
here.
So what do we have at the very beginning we have all of the imports for the modules we need to use.
And we have three functions.
We want to take a photo, one to update the photo log file and one to send the photo by email.
All right.
Then we have a bunch of global variables.
And then well basically when we start to run this Python program is going to run all of the codes here.
So it's going to set up the email.
Then it's going to set up the camera.
Then we remove the log file if there is one.
We also set up the Gpios.
And then well we have those two functions.
So those two functions are here because they are directly related to all those callbacks here that we
register.
And in those functions we also use the global variables.
Okay.
So that's why I put them after all of this initialization.
And so after we initialize everything we register the two callbacks and we pause the program.
So we are just waiting for those events to happen.
I'm just going to add a new log here just before we pause to say everything has been setup.
Okay.
So we know that's the end of the setup.
And now to test the program I will need to write something valid here.
All right.
So I will replace that with my own email address.
I'm going to save with Ctrl S and then let's run the program.
Okay.
So let's stop that one.
Let's run the project step for.
Email setup.
Okay.
Then you see.
Camera setup okay.
Previous log file removed Gpios.
Okay.
Everything has been setup, so let's make a movement for five seconds.
Okay so it took a photo and you see sending it by email.
We didn't get any error which means it probably worked.
Now let's check the emails.
And I just received a new email here.
So the last one you see zero minutes ago and what is quite blur because I'm also moving my hand here.
So that's the photo that I have received.
You see new photo from Raspberry Pi.
Check out the new photo.
So it correctly worked.
And every time you see I have a new one.
So every time I receive a new photo here when there is a new movement detected.
All right.
And that's the end of this first program.
So that's the end of step four.
And then in the next step we're going to start the second program for this project.