Sending Email using Python in 5 statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what if you want to send an email but you don't want to use a gmail client you want to create your own client using python yes that's possible you might have heard about this right using python you can automate a lot of stuff like uh running some files or uploading some files or clearing the garbage from your machine and sending an email everything is possible using python basically you can automate stuff so in this video let's see a simple example using python how can you send an email now of course i just want to keep it very short maybe five six lines of code it is not actually a secure way of doing it we are doing it just for two things learning and to have some fun with python okay uh how do we do it now first of all you need an ide and a python setup so in my machine i have python 3 version in fact i have python 2 and python 3 as well it's just that python 2 is by default and that's why i have to specify python 3 here because by default it's python 2 for me uh i'm lazy to change that setup but anyway if you have python 3 by default just use python otherwise you can use python 3. so we'll be using python 3 to run this and what else we need that's it let's start coding here now what you need to send an email using anything in fact for a gmail client as well you need a username and a password of course you have to log in first right now after logging you can actually run the code you can actually see the emails right here what you need is if you want to send an email the first thing you will do is you will say login now what are the two things you pass you pass a username and a password right now just for this particular video i have created a dummy account on gmail uh the email address is telisco naveen and gmail.com and the password is something which i will reveal in some time don't worry before uploading the video i will change the password so don't even try to recover that so going back to the code here we have to pass the username the username is the disco in fact the email address actually that is called naveen.gmail.com this is not my usual account which i use this is just a dummy one for this video and the password is just dollar for dollar demo this is the real password uh for this account okay as i mentioned before i will change the password later that's it you are able to log into your gmail just by using this line maybe you can see there is still an error on the screen the next thing you need is you have to send an email right to send an email we'll be using a function called send mail in which you have to pass three things the first one is who is sending the mail second is who is the receiver of the mail and third one is the message the actual message so here in this case i will say telisco naveen gmail.com that's the sender of the email the receiver of the email is uh okay where do i send it so i will send it to my official account uh the disco account is connected.com this is the real email address which we use and the third one is our message say i will say okay mail sent from python code any message will do okay that's it so things are needed and when you're in this code it will send an email now first of all i want to show you the real account which i use for my corporate account so if i go to my mail app you can see uh this is the real account and actually i'm hiding all the other emails i'm only showing the unread mails here and as of now there's no messages zero messages which are unread okay so you can see this is the email address connect at the telescope.com so we'll be using this okay let's go back to the code and let's try to underscore what you think will it work of course not you can see there's an error on the screen itself and that's why i hate ides they will show you the actual error on the screen it says login and send mail not found okay for before that i will do one more thing i will say print mail send on the console itself and you can see there is no error for the print because print is a function which you can write anywhere but these are not these are the function belongs to some particular library or api you can say now which library i'm talking about do we have to use an external library now that's the beauty about python right python does support external libraries you can import them or you can use inbuilt libraries of python itself now one of the library which we are going to use here in fact that's the only library we are going to use and that is smtp lib i know that's weird that's a weird name right why smtp lib why someone would keep that name so the thing is smtp is actually a protocol to send a mail it actually basically works with email service example let's say if you want to send a file you use ftp as a protocol if you want to access the world wide web we use http as a protocol if you want to send some packets you use tcp or udp in the same way if you want to send an email we use smtp which stands for simple mail transfer protocol so we can use this in fact we can also use because this in this code we are basically sending an email we are not receiving the email right so when you are sending it you use smtp for the saving i have done that in java we use imap and pop or pop we have a choice as of now let's use smtp and let me know if you want some more videos on email service in the comment section now once you got this library we'll create a object let's say server now this object has these two methods right login and send mail but which object it is so it is actually smtp library so it has smtp lib dot there is a class called smtp so if i jump to this class you can see it has so many objects and functions so if you scroll down there's so many functions here but we're going to use this to create an object now what exactly we have to pass here so we have to pass two things the server address smtp address for gmail and we have to also mention the port number now this server address and port number will change based on which service we are using so here i am using gmail if you are using some third party email service example for my corporate account i have a third party service and for that they will be having a different server address and a different port number now for gmail as most of us have a gmail account so we have to use smtp.gmail.com and the port number is 587. that's it once you mention these two things uh we can actually create a login so we can say server dot login and we can say server dot send email will this work let's try it out i hope this will work what do you think let me know in the comment section so give our timestamp and mention will it work or not how do i done this so we have to say python3 a mail demo okay mail demo dot b y enter and we got an error the error is it says smtp auth extension not supported by the server see the problem is before you even log in you have to make sure that you have a connection with you now how do we create a connection so we have to say server dot start okay but then we want to create a secure connection right so for security we either use ssl or we use tls so we will be uh we are going to use tls here which is transport layer security we can use that and yeah that's it what do you think will it work now it should since i'm very confident about this one let's try and okay it's taking some time at least if something happened it worked you can see it says mail sent how do i verify that so what i will do is i will go back to my app uh and okay there's no message yet this is weird it says my mail sent there is no error as well so we'll wait for the mail in fact there is one more thing one more setting which you have to complete and that setting in fact let me try this one maybe something went wrong i will say just for fun i'm changing my password is it really working okay so if i change my password it is giving error that means something is happening there let me just clear the screen and run this once again okay it says mail sent and i got my email on my ipad but not here as of now what's wrong do i have to reopen my mail service no email yet okay so you can see i got two emails actually there was some issue with the refreshing you since we have run it second time we got it here so you can say it's mail sent from python code this is working okay now there is one more setting which i forgot to mention when you use gmail uh so make sure that you do this so manage your the problem is we are using this service from a non-gmail account right non-gmail application so gmail account but not gmail application and by default gmail will block it so if you you have to enable it right because gmail is very secure the way you enable that is by going to a security tab and if you scroll down you can see this option of less secure app access by default it is on uh actually it's not by default on i kept it on while i was testing it so by default it will be this state for you this is how it look like on your machine right so you have to say turn on axis and check it again not recommended this is not a production ready code but just for the example you're doing it so make sure that you enable that otherwise if you don't enable it and if you're trying on this code it will give you an error you can see this is whether you will get so make sure that you check it and run this code so i hope it makes some sense how do we automate a sending of email so basically you have to import this library you have to mention the server address and the port number start the secure connection log in send an email again not a person redic code what if you want to send an attachment what if you want to have a subject you can say we don't have a subject for this email where is my mail app so you can see there is no subject here what if you want to mention the subject for that we have a different code let me know in the comment section if you want that video as well so that's it from this video i hope you enjoyed let me in the comment section and do something for the videos bye [Music] you
Info
Channel: Telusko
Views: 11,454
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: BsVQ_cBmEwg
Channel Id: undefined
Length: 9min 52sec (592 seconds)
Published: Fri Dec 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.