today in just a few lines and with a
free gmail account I'm gonna show you
how you can send email and text alerts
to yourself in any program you do from
now on 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 that'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 so as you
can see here I have Visual Studio open
that's what I'm gonna 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 spider 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 gonna do here is we're gonna set
up our script and then we're gonna
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 gonna create a
function because I want to be able to
import this into other programs or do
different things with it so I'm gonna go
ahead and create a function I'm gonna
say def I'm just gonna call it email
alert and inside this function I'm gonna
pass it the subject which is gonna 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 line
very above so I'm gonna just create a
variable called MSG you call it whatever
you wanted and I'm gonna say email
message and that's gonna create my
message from this library on my message
I'm gonna call a method on that object
and it's going to be set content and I'm
gonna 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
gonna 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 gonna 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 alright 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
gonna go ahead and create a variable for
my gmail user I'm just gonna 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
gonna go ahead and up here I'm gonna
also set the subject of my email that I
said above so I'm gonna say MSG subject
I'm gonna set that equal to whatever I
passed into this function for my subject
and I'm gonna do something similar for
the two variables so I'm going to say
MSG two and I'm gonna set that equal to
whatever I passed in in the two
parameter up above so now we have our
email set up we have our password set up
now we have to hat set some server
parameters so I'm gonna set my server
equal to and now is where I'm gonna use
my SMTP library from above I'm gonna say
SMTP Lib dot SMTP and I'm gonna set it
to Gmail's SMTP server which is SMTP
gmail.com and I'm gonna use port 587 now
I can work with this server variable
that I just created so I'm gonna say
server dot start TLS which is just a
setting Gmail requires and then I'm
gonna go ahead and
try and log in so I'm gonna say server
dot log in and I'm gonna pass it my
username in my password so I stored
those as user and password
and then after it's done I'm just gonna
say server quit so now I'm gonna write a
quick test method so we can test this
because it's actually gonna fail right
now so I'm gonna 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 gonna run it so
it's not always gonna run this test if
we import this code into another program
or something like that so here we're
just gonna try and call our function so
we're gonna 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 two last might be different than how
you would have done it and you can
switch it around however you would like
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 gonna use my I'm
gonna send it to my real email jet
address Jake at clarity coders comm 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 setup 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 my
account google.com once you get there
we're gonna go to security and we're
gonna set up 2-step verification now you
have to do this in order to get an app
sorted and you have to have an app
password to use a third-party app to
send email 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 it 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
gonna replace my old password with that
new app password they gave me again
remember you have to use all your own
stuff I'm gonna 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 gonna say msg from equals
and I'm gonna 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 screen shot 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 ten
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 app v-tex comm so I'm gonna
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 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