Create email and text message SMS alerts with Python!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in just a few lines and with a free gmail account i'm going to show you how you can send email and text alerts to yourself in any program you do from now on now i use this to check if servers are alive at work to see if my bots are still running and sometimes as an air check in my programs and i just import it from a file like this before i forget if you haven't joined please join our discord we got a totally free no advertisements discord it's just about programmers helping out programmers and don't forget to subscribe to the channel it really helps i just have this small channel so i appreciate every subscriber or like or comment let's not waste any more time and jump right in [Music] so as you can see here i have visual studio open that's where i'm going to write my script you can write your script wherever you would like wherever you want to edit python if you don't have python installed you can reference this video to install anaconda it will get you python installed in spyder ide so you have an ide to write code in and the language itself installed and then you can follow right along with me so what we're going to do here is we're going to set up our script and then we're going to create our gmail account we have to do some things to change around in gmail so it'll allow us to send through our program so the first thing i'm going to do is import some libraries here the first one is import smtp lib the second is from email.message import email message and i think that's all the libraries we need to get started here so if you run this program it should import both those libraries successfully if it doesn't you need to install these libraries if you haven't used them before so all you need to do is do pip install and then the library name once you have those two libraries imported and installed we can go ahead and create our function now i'm going to create a function because i want to be able to import this into other programs or do different things with it so i'm going to go ahead and create a function i'm going to say deaf i'm just going to call it email alert and inside this function i'm going to pass it the subject which is going to be the subject of my message the body of the message and the to address now i'm going to create my message itself from the library above so i'm going to just create a variable called msg you could call it whatever you wanted and i'm going to say email message and that's going to create my message from this library on my message i'm going to call a method on that object and it's going to be set content and i'm going to set it to whatever i passed in for the body of the message now i need to set up my email and my password i'm going to use so i'm going to go ahead and create a brand new gmail account you can use your gmail if you want or you can create a new one with me but do note as with my other videos i'm going to just show you my password here this is not my real password i'm going to change it after this so you have to create your own account don't try to use mine all right so i went ahead and created a new gmail account so i could show you some of the things you have to set up here so i'm going to go ahead and create a variable for my gmail user i'm just going to set it user equal to and then my new email address as you can see it is a cool one and i'm going to set my password equal to and the password to my new email account i'm going to go ahead and up here i'm going to also set the subject of my email that i set above so i'm going to say msg subject i'm going to set that equal to whatever i passed into this function for my subject and i'm going to do something similar for the two variables so i'm going to say msg 2 and i'm going to set that equal to whatever i passed in in the to parameter up above so now we have our email set up we have our password set up now we have to ha set some server parameters so i'm going to set my server equal to and now is where i'm going to use my smtp library from above i'm going to say smtplib.smtp and i'm going to set it to gmail's smtp server which is smtp.gmail.com and i'm going to use port 587 now i can work with this server variable that i just created so i'm going to say server dot start tls which is just a setting gmail requires and then i'm going to go ahead and try and log in so i'm going to say server dot login and i'm going to pass it my username and my password so i stored those as user and password and then after it's done i'm just going to say server quit so now i'm going to write a quick test method so we can test this because it's actually going to fail right now so i'm going to say if underscore underscore name underscore underscore equals underscore underscore main underscore underscore so what this is doing it's saying hey if this is the actual main program that's being run go ahead and run this code here and if it's not the main program we're running it's not going to run it so it's not always going to run this test if we import this code into another program or something like that so here we're just going to try and call our function so we're going to call email alert now we chose the parameters of this so we have to follow that order we could have chose any order again me putting subject first and to last might be different than how you would have done it and you can switch it around however you would like but we're gonna follow our structure that we set up here so the subject i'm just gonna say hey the body i'm gonna say hello world stay pretty classic there and my test email i'm going to use my i'm going to send it to my real email jet address jake at claritycoders.com that should be it so let's go ahead and run this and you'll see we get an error it says our username and password are not accepted so to set up our gmail account to use third party apps we have to do some extra stuff to our email and i'm going to show you that here but basically we need to set up two-factor authentication and we also need to get an app password instead of just our regular email password so while you're logged into your account that you're trying to send this email from so whatever that is if it's your normal email or a new email that you set up we're gonna go to this url which is myaccount.google.com once you get there we're gonna go to security and we're gonna set up two step verification now you have to do this in order to get an app password and you have to have an app password to use a third-party app to send emails so we're gonna go ahead and click that get started you're gonna have to log in again you have some options here you can do whatever way you would like i'm gonna go ahead and just send myself a text message and you can see that it worked i can go ahead and turn on my two-step verification now once you've done that you can click this little back button and now you can see we have a new option here we have an app passwords so go ahead and click that have to enter your password again and i'm just gonna set for my app i'm gonna say other i'm just gonna call it email alerts for me and generate and you can see here that it gave me an app password to use so i can grab that go back to my program and i'm going to replace my old password with that new app password they gave me again remember you have to use all your own stuff i'm going to wipe out this account and from here you can go ahead and run this and it would help this email address was actually with dots not with underscores and we also need to set the from where this message is coming from as well so i'm going to say msg from equals and i'm going to use the same email that i had below here so i'll actually move this down below and then i can use that variable and we forgot the most important part we need to actually send our message so we're gonna do server dot send message send underscore message actually now as you can see here from the screenshot of my phone you can see that i got my email alert at this email address now i told you earlier that we could use this to send text message alerts as well so i want to show you how to do that really quick you check out this website that i'll link here it shows you different carriers what email address you can add on to your 10-digit phone number if you're in the united states at least to send a text message alert so i have verizon so i can use this at vtext.com so i'm going to come back into my program i'll clear out my number for you guys but and you'll see that just making that little change you can see from this screenshot i get a similar exchange except this time it came to my phone as a text message instead of an email alert so you can use it either way now you can simply import this function in any project you have and set it up to send email alerts if you want to see more of that i can do that in the next video for right now you should have a working email and text message alert system again please subscribe if you can if you have any questions leave me a comment hop in the discord if you want to chat live with me and other programmers like us and until next time keep coding
Info
Channel: ClarityCoders
Views: 140,021
Rating: undefined out of 5
Keywords: Python, python projects, beginner python, python alerts, automation email, python programming, python gmail, python send email, python gmail send email, how to send sms using python
Id: B1IsCbXp0uE
Channel Id: undefined
Length: 9min 49sec (589 seconds)
Published: Sat May 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.