And G.M. SMTP server connection.
Hi, guys, since last time we were talking about water, the basics of the asymptotic connection with
the emails, today we're going to write a program that will basically send emails from one Gmail to
another.
We're going to connect the Gmail server and then we're going to exchange messages between the different
users.
So today we're going to use the advantages of the Gmail Skype server to send an email.
And so if you're giving Gmail, um, you can use yours or if you don't, please guys create one Gmail
account and the only thing you need to know is your username and password in order to do these tasks.
So if you don't have it, please go to Gmail dot com and like this here, go to Gmail dot com and there
you'll be able to use your username, your mail and so on.
So now I'm assuming that you've already created your Gmail.
And so after you did this, let's go to our resources folder in PI Charm and let's actually create a
new folder inside, which will be called Section five.
So create a new directory.
That's it.
And it will be called Section five.
OK, so here is Section five.
Let's create a new python file in the python file will be called s.M t.P, Underscore and Gmail.
OK, so now you're in the writing mode and I will zoom in a little bit so you can see better.
And the first thing I will do is of course to other Python directory with uh.
Yuzu and then Bill and then Ian ve.
And in Python three.
OK, so after you do that, let's first import all the necessary libraries for that project.
So what we do here, our import, import the library, the same type of library as you can see, it
already exists in the circuit library.
OK.
And of course, after I'm done with that, Lizardo from s.M t.P Library
import s.m t.p ssl us assim t.p.
OK and then Lizardo from email dot mym dot text import my text.
OK, so basically this invokes these two lines.
Invoke the secure SMTP protocol which is on import 465.
It uses the SSL server OK was already without the next lines of code.
I would like to write them with an exception so we can ensure that if an error occurs we can encounter
it.
So let's do here, try and get a message for message equals to my text and then let's write a message.
One say hi.
OK then I'll write here
plain OK.
And then let's write m g which is our message and then let's write here a subject.
OK, so bear in mind that the M is G is no object from the main text method.
So here with a text middle to parse this parameters and we are outputting the created message.
OK, so subject is equal.
Uh message for Martin y ok.
And then the right Asaji.
Amadeu, and then from and then let's write this email p y Yanov, which is actually the name of my
first work with Chris Mayer, with his name at Gmail dot com.
So let's say he wants to send me an email.
This is what we are going to simulate here.
So this will be the from users or from this user.
We are going to send an email message.
Now, the next thing I will do is to create the assembly position.
And basically the assembly session is quite easy to do.
You basically need only one line you simply need to write.
A simple tip equals s.m t p library.
OK, DOT, SMTP and SMTP is a class from this A.P. library.
So here we create an STPI object which will generate the assembly position.
OK, so as we talked in our introduction to videos now here, since she declared there SMTP, you need
to define the server that you want to connect.
If you remember the graph from the previous video, you need to connect the server.
So our server is going to be SMTP, dot Gmail dot com.
OK, and then we're going to call the server on port five hundred eighty seven.
For instance, OK.
Now, one thing that is not necessary to do, but I always do it in order to ensure that everything
is OK with the court, is that the bugging?
So the S&P actually has a quite nice function and you can simply write s.m, keep dot set and then underscore
and then Tiberg level, OK?
And you can simply say just to true.
So this means that our method will be completely the book.
And finally, I will do the same t.p dot eho.
And this is usually done in order to identify ourselves as Jhilmil client.
OK, and for that reason, because we want to define ourselves as the client of the house that we are
assessing for that reason we are going to the actual client.
OK, once we're done with that, we're going to write a single line for encrypting the session.
And this is quite easy and let's create a new flow.
So if SMPTE p dot extern for external start here was OK, and if you're wondering what is the start
of this, basically there's a twist.
So let me actually write here.
The T.
S is a is encryption protocol.
OK, so here we're going to check if we're able to encrypt the message.
So if we have the encryption protocol, we would actually prefer to encrypted because it's more safe.
So you can write a SMTP dot start to W and that's it.
Here you are simply securing your email with the worst encryption and deal with encryption is basically
similar to any other encryption.
It simply gets your message, it generates a key and generates a main source so you cannot open it and
you have the key.
And once the email has been sent, this basically unlocks the encrypted message.
So on the way there, just nobody can access it.
OK.
And after that, we would need to identify what was sent after the encryption.
So we are doing that in order to make sure that the message that we received is the same as the one
we send it after the encryption.
So if red SMTP dot wall, this will actually check the message.
Once it is, send it in after the encryption.
Now, on the same the tension level as the IF statement, I am actually going to try another thing.
So let's try and our block and let's add some space here so we can distinguish it.
So let's try and then SMPTE p dot login in here.
Guys, with the login function here in the brackets, you need to provide the user name and password,
which means your email address.
For example, here, my email address is P y Yanov at Gmail dot com and after that in parenthesis provide
the password.
Obviously I'm not going to show you the password of my email, but once you do this for yourself in
that field, just add your password as it is just right in there, OK?
So in the log in credit, those two can have your email in your password.
So once you're on the code, your email is going to be assessed simultaneously.
So for now, I will not allow it.
I want it later when they run the code.
So don't you worry, this code will run, but when you're creating it simply out there, your password.
So we're trying this and if it doesn't work, we can do except and we are going to write a SMTP library
dot s.m extension as E and here we can print.
So this means if we fail to login, we'll just say authentication failed or indication error.
That's right.
Colma e right.
And after that we will write sis dot exit one.
So this means guys, if we have a problem with our creditors, we are going to receive the right error.
So the.
We know exactly what happened, so let's quit on our blog, so let's do try.
OK, and here we're basically going to send the email.
So until now, we actually connected to the server here and then we encrypted the message was that the
encryption protocol, then we added our credit those.
So right here.
So you're actually aware adding created those.
OK.
And after that, uh, after we pass this section, we're now going to send the email, sending the email.
OK, and this right here is SMTP dot sendmail.
And then what you need to do here is to write the email from which you're going to send the message,
which is pure white Yanov at Gmail dot com.
OK, then in the brackets, write the email to which you want to send the email list.
Right, MPLX Yanov at Gmail dot com.
OK, which is my email and then am S.G. Dot.
So images and messages that were created earlier and then US string and that's it.
We're ready here on this line.
So if you wonder what is the image, you will be created right here.
And this is basically our message, which has a topic in which he has a sender and which has a text
inside.
OK, so we simply refer to it.
And now if this don't work, so if we fail to actually send the message, we are going to write except
and then socket dot gate error then socket.
That error, and then we're going to create another circuit age error and then we're going to write
A.S.A.P. Library DOT is empty B exception.
Mm hmm.
So this is how we're preventing ourselves from that errors.
So this is going to be us e and let's try it on the next row.
So this is supposed to be the exception in the next rule we can write print.
So if we get exceptional print and then write your message.
He has not been sent.
OK, so you get this message, if your message does not send it in some way, then will print he OK.
So we're going to print all the errors and then insist that exit and the result will be one.
And finally, we can write finally and then quit.
Actually, SMPTE people need to quit the protocol and then quit.
That's it.
So we're ready here.
Guys, this is how we're sending the email and how we're preventing it from unexpected errors.
So finally, let's finish by exception.
So I write except and if you can see here, this whole block of code is also a trite statement.
So I will need to do something similar as this, except look here, the previous one.
So I will do socket dot dot gate error and then socket dot error, then socket Donald H error and then
s.m type library.
Does it seem to be extension that set us e OK, perfect.
And finally, let's try to print and in the print statement our basic right to send a message.
So here we are going to write an error or what's going to appear if the try block earlier on don't work.
So C message has not been sent.
OK, we are ready here.
Let's write some exclamation marks then let's print E!
Sorry, and then we will exit on one sees that exit and then one.
OK, so guys, we're ready with the code.
This was the whole code.
Let me show it to how long it is exactly.
So you can see this is the whole code here.
And now let's go to the terminal where I will show you how to run it.
So here I am in the terminal in the resources folder.
So to see the Section five airways and you can see that we have only the centipede underscored G.M.
protocol.
So as we're as Section five.
That's right.
Python, Python and then jumbled dot, pure white and let's see if it works.
So actually, on to here, we're getting the connections.
But there was some error on line 18.
It is SMTP object has no attribute.
So let's go back to the code.
And actually, yes, here is not here, but instead it is has exten, as you can see here, it's now
predefined.
So I remove the previous one and I will save it.
Let's run the code once again and see what happens.
So I write Python is empty B at Gmail dot com or Gmail dot pure white.
OK, we're getting no authentication error and actually does not surprise because I don't yet added
my password here.
So let me add my password from another terminal or another tab and then I will try it again.
So now I added my personal password and let's run the code again and see if we're going to face any
other errors that we'll need to fix.
Xiaodong the word if we are facing errors, this is actually quite good because we can see how easily
we can debug them and you can kind of get the feeling that the errors are not something that bad because
everything is quite descriptive and you can very easily fix it.
So let's run the code again and see.
So as you can see now, everything looks fine and you see the different communication messages here.
You don't have any errors, right?
So you can see the reply, the same status, the post that you're opening.
So for example, here, the 220 is ready and 220 is the power that we are sending with.
You can see that here we open the server connection because the SMTP, Gmail dot com is at your service.
So it open the port and here we can see all the rest.
But actually, there's only one proof if everything worked.
So we as the only way is simply to check my email.
So as you know, we stated, though, that the message should be sent from one P y Yaniv and dot com
to my MP Yaniv at Gmail dot com.
So actually, since the courts accepted by the system and I can see the message sexual send it, let's
do it once again while my email is here.
So here we are in my Gmail and you can see now I'm going to run the code Python A.S.A.P. Gmail, not
P white.
So let's try it and let's see if I'm going to receive the message at the same time.
So I'm going to run the code and you can see that I received a new message from my Gmail immediately.
And the message has a subject message from Martin White in the same way as you wrote it in our script.
And you can actually see our script here.
So the message for for Martin White was written here.
So this is a subject and here's how White in the subject name.
And then we have a message, one CIHI, which is actually what is inside the message.
So this is a very easy way, guys, to send automated messages to multiple people in emails.
If you desire, you simply establish the connection with the Gmail.
And then it is very easy to send your message in the book for errors.
So that was everything that they want to share with you for today.
Guys, I hope you enjoyed it and you are able to actually send an email with this script.
So that's it.
Thank you very much for watching and bear with me in the next videos because we are going to do some
more fun stuff with email protocols.
Thanks for watching.