Sending Email (Account Activation). Django rest framework project tutorial. [4]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right guys welcome back so in this one we'll go ahead and send an account activation email to the user so that they can activate that the accounts and get access to the application so when it comes to tokens we are going to be using this mode you called Django rest framework simple JWT so it is actually going to be able to like manage our DWG authentication scheme to easily give us like refresh tokens for specific users and then I want to use an access token to to actually tie it to a specific user and then use it to verify by their account so right here I'm going to stop the server and then install this package so here on it's documentation so once you install it you actually need to setup a key in your risk framework configuration so I'm going to cut this one out so it has installed so if you come back to application and go to settings which py in the rest remark configuration so ready I'm gonna put a comma and then define this so this is gonna be the fourth of education classes we wanted to use a Jedi written class that we've that ships with the package we just installed alright so once we have this now we need to actually send an email to the user once we create their console for us to be able to do that we need to take Django about a few things so this will enable it to know how to send an email so the first thing will be email use TLS so of course this is going to be true and then the next thing will be the email first email false so this will be the SMTP server we use of Russia are going to use SMTP gmail.com yeah but you can use can you send grid you can use the Amazon services depending on what what you want to do so use Gmail call sorry is it SATA now we need to define like the email pods so email port and this is five eight seven now we need to provide to authenticate as so you can do it using an app password but what I'm going to do is I'm actually going to use an account password so that I wouldn't get on behalf of the application so we're going to need you email host user so this one is gonna be I'm actually going to use my credentials the ones I use them for my Google account to be able to do this so yeah so I have an email called test sender 100 at gmail.com okay and then we need to define a password for this so this will be e mail host password I'm gonna copy this email host password alright so this one basically will be the password of my own I'm not gonna clip it as X exits feel free to use your own and now once we have this now we need to actually ghost its send an email but I don't like having these details in my settings that py file that's when I push this to get everyone's going to be able to look at my emails and start spamming me over the Internet alright so what I'm gonna do is I'm going to use the OS module to pull this one's from a virtual environment so here when I use all is good in there let's get and then I'm gonna call it ma host user so here my main application folder I'm gonna create a file called SF and so I'm gonna define that so email so I ain't got em to define the variables wrapped right export in your keys email post user it's gonna be sharing my email so once you have this I'm gonna duplicate it for the password you may host password so for now it's gonna be only access okay but you use yours don't use that don't-don't-don't keep this as it's gone it's not going to work keep the stars unless your password is vistas which will be awesome okay so I want to move this onto the main projects route ok so right there looks good so now you can do the same thing for the password you may post password you can do the same things for all the environments you don't want to expose to expose to get like your database credentials and the right now right here you know views dot P why we need to actually create a token for the user and use that to send it to their email so I'm going to import something from risk remarks simple generating with it yeah like that the tokens import this one's gonna be code refresh token now for us to be able to create a token you can define token here the token will be refresh token then we call the method code for user and then we give it a user so I'm gonna set up user here so for us to be able to get a user Australia country user because user which we need to bring in model cause we need courier service action so from that models include user so down here we're going to user objects just get now we can get by email so email will be it's a data then email cuz I'll show by the time it gets here then to have a user created so it will give us our email our emailed it for the new created user and then we create to get which use IDs so this actually gives us access to the user and we can create a token using for user alright so that should give us a token but the way it works is it actually gives us two tokens the Refresh and the access one I'm going to creative a class here so that it can help us to do extending anime on the lake so now in here I will define the tasks for you till we need to define let's define a method called send email so depth st. email so I want is to be a static method so I'm gonna use this decorator so static method so this helps us to actually use this class method without instantiating the class itself so I'll show you how that works so send email basically we will take in some few information so one will be data another one will be yeah so it will be only data we will do all almost everything goes from here all right so now right here now that you have the token we can actually - so we have the class good to remain a de pasiĆ³n so now that you have the class you can import it so from gto import with you alright so here you can actually do you to let send email and then we need to pass it in the data so notice how I didn't have to do like it here it goes it here then util dot send email I just need you to lose anymore this because this even is mocked as a static method alright so now let's define our data so in our data here so this is gonna be I'm gonna use a dictionary so the first one is gonna be the domain so for us to be able to get the domain we run gone cuz if you think of it we want a user to be able to click on a link in the email and then the link should be able to bring them back to application so right now for us to be able to get the Ring be able to get the domain we need to use the site's framework so from Django contrib both sides the side stood shortest input gate current site alright so here you can do like current site we're gonna be get current site and then this text in a request which we have so request and then frost we would get the domain we can do get current site domain let's bring it here put domain all right so that gives us the domain where we want to go but now we want to have the page where they go to when they click on that image so that will be another API view so actually mr. erupt here so let me have my creative link relatively think relative would be uh huh so we know let me make it empty so we need set up a class to verify they use actually to class so class verify email so this one is gonna be heading from post generics no generator the generic a behavior so now I'm going to have a gift request so they're get text in a request all right so so now we will have this and what it will do basically let me actually add a pass so for now it will be just a class so for us to be able to get where to put to redirect the user we set up a URL for it so I'm gonna add the comma here copy and then the path is going to be email maybe I should verify so let me bring it in here verify email verify email all right so give it a name too all right so once you have this now we can get the URL to this file so we can do that one using reverse sorry I in views I'm going to import to reverse so from jungle rivers import purpose okay so robust does is it takes in a a URL name in this case will be emailed verify and then it should be able to give us the path so if we come back here now here we can do receive link it was reversed and then tip you name will be emailed verify alright so once we have this now we can construct our absolute URL so gonna do absol this is gonna be the domain of which in this case the domain let's say oh this should be domain did it so this actual returns us something like google.com without a protocol so I'm going to construct a protocol here we are going to have HTTP concatenated on there and then we are going to concatenate our domain which we have in Oh also let me actually bring this one out bring it here and I'll only keep this okay so we will concatenate get current site and then we want to also concatenate this view so you can add like loads the link and then here I wanted equal program so I want to do something like token equals I know they took and sowed to get it open it's gonna be here in token like that then I said it returns a dictionary to want to access the access token so I'm going to bring this one off so this one should be equals and then plus and then for the token so right here I'm going to add that access yeah let's do I get only the access token all right so I believe oh this should be good access token all right so once we have this now we can say send email and then we give it that so for now you can see that we have so unknown data we have current site right here so we have the domain but now that we have the domain which I'm going to actually press with absolute URL so now we need to define a couple more things so one is gonna be the subject so here you can do subjects so the subject will be like verify your email and then since we are passing their domain to see a passing the domain you will be able to construct this one beta but what I want to do is actually do it here I thought you may only does things to do with the email keep sending the email so I'm gonna keep both here so I'm gonna say this one to make sure it prefixes with my email body is gonna be in my body alright so here down here now we can do email underscore body so this is going to be equal to note and constructor message so I I'm sure you can even add the name by doing user let's username and then because you can concatenates something like use link below verify email so now I'm going to add a new line down so now I can concatenate our link so new line and then the link also the messaging I think a bra bra bra use link below to the Friday our email and then on the new line we have the link that we have constructed from here alright so so now in here we have setup the body we have set up a subject let me actually remembers the email subject alright so once we have these two now we can come to avoid chills which is right here now we are going to be using the email class that email message class so just import it so from jungle core let me input email message and now here we can do something like email it cause email message so you can see MMSE tech takes in the the subject and then the body and all the other ones can be none okay so we already have the subjects in data so you can do subject equals data then we want to look out for email subject and then comma now we need to have the body so it is gonna be that and then you can substitute this one for body no this here we need a closing bracket so once we have this now we can do email put sync all right so once we have this now we can actually test it out so so going back to our postman I'm going to change the email to something else then click post let me also change the name to use the name click post Savva is not on let me bring it up again try again click send alright so we actually have an issue you cannot concatenate a steer can only concatenate this year not access token all right so here in the views see how when we were concatenating by talking it should be access token so I'm gonna turn it into a string by doing STR game so let's try again click on save it's back try again choose another email is the name st. topo has no I should same okay so here in our YouTube that py so we defined email message we gave it our details and then we forgot a comma at the end damn it so that turns everything into a tuple so that's not what we want to do let's try again she's another one another one another one st. and you can see that now we get the email being oh you can start we get the user created when I want to be able to test if actually the email was sent oh we actually forgot something here so one of the key things we need to do is added to so this is where the email will be going so to now we need to define 2 so 2 will be the user's email itself so let's send it yeah now I data I'm going to copy this duplicate it and then this one is gonna be to email and then to email is gonna be user dot email alright so now here in the u2's in sent mail we can define so to actually takes in a list of recipients but what we want now what we want to send to one user so we can do data do it to email to something like that alright so let's try again choose another user by user name st. and yeah so now you can see that it actually takes a while to give us a response so let's see yeah so you can see that there is an issue this is an authentication error so make sure that in your dot M file you have your correct credentials which might actually run at the moment but I'm going to connect them right and then I'll show you what to do next furgus so when I talk my account password in dot F so once you do that you need to make sure that your dot M is being can it can be can be read so here if we do an LS and do it is LA because that we have a good aim so you need to make sure that you so see team by being sauce put in and then run back the application now if we try again in postman so I'm gonna use actually anime that exists price a craze at gmail that's fine Christ good so now let's try again click send right to after where we can see that we get the user created and then if you wanted to say they check your email to check your account to verify your email you could do that so like here in your views put py so here you can actually create another key and then use a data and then send that so let me check my email to see if it actually worked alright so you can see that I have verify your email coming in if I click on it you can see that I have five my username used a link to verify your email and then it's going to our correct domain then our correct view and then the token is actually here so you can see that the access token is actually added to a URL so now in the next one we will go ahead and activate the users account if you enjoyed the video can we give it a thumbs up subscribe to the channel I'll see you in the next one bye
Info
Channel: Cryce Truly
Views: 46,204
Rating: undefined out of 5
Keywords:
Id: BXg-b20Xusw
Channel Id: undefined
Length: 21min 35sec (1295 seconds)
Published: Thu Jun 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.