1 00:00:04,860 --> 00:00:08,290 And G.M. SMTP server connection. 2 00:00:08,700 --> 00:00:16,120 Hi, guys, since last time we were talking about water, the basics of the asymptotic connection with 3 00:00:16,120 --> 00:00:24,540 the emails, today we're going to write a program that will basically send emails from one Gmail to 4 00:00:24,540 --> 00:00:25,040 another. 5 00:00:25,050 --> 00:00:31,320 We're going to connect the Gmail server and then we're going to exchange messages between the different 6 00:00:31,320 --> 00:00:31,920 users. 7 00:00:32,490 --> 00:00:39,930 So today we're going to use the advantages of the Gmail Skype server to send an email. 8 00:00:40,140 --> 00:00:51,330 And so if you're giving Gmail, um, you can use yours or if you don't, please guys create one Gmail 9 00:00:51,330 --> 00:00:57,200 account and the only thing you need to know is your username and password in order to do these tasks. 10 00:00:57,470 --> 00:01:06,510 So if you don't have it, please go to Gmail dot com and like this here, go to Gmail dot com and there 11 00:01:07,020 --> 00:01:10,590 you'll be able to use your username, your mail and so on. 12 00:01:11,010 --> 00:01:14,700 So now I'm assuming that you've already created your Gmail. 13 00:01:15,390 --> 00:01:24,180 And so after you did this, let's go to our resources folder in PI Charm and let's actually create a 14 00:01:24,180 --> 00:01:27,090 new folder inside, which will be called Section five. 15 00:01:27,420 --> 00:01:32,100 So create a new directory. 16 00:01:32,270 --> 00:01:32,940 That's it. 17 00:01:32,940 --> 00:01:36,420 And it will be called Section five. 18 00:01:36,750 --> 00:01:39,720 OK, so here is Section five. 19 00:01:39,970 --> 00:01:49,710 Let's create a new python file in the python file will be called s.M t.P, Underscore and Gmail. 20 00:01:50,100 --> 00:01:56,470 OK, so now you're in the writing mode and I will zoom in a little bit so you can see better. 21 00:01:57,240 --> 00:02:04,540 And the first thing I will do is of course to other Python directory with uh. 22 00:02:06,680 --> 00:02:12,600 Yuzu and then Bill and then Ian ve. 23 00:02:14,930 --> 00:02:18,110 And in Python three. 24 00:02:18,440 --> 00:02:24,780 OK, so after you do that, let's first import all the necessary libraries for that project. 25 00:02:24,800 --> 00:02:35,960 So what we do here, our import, import the library, the same type of library as you can see, it 26 00:02:35,960 --> 00:02:40,040 already exists in the circuit library. 27 00:02:40,280 --> 00:02:40,730 OK. 28 00:02:41,000 --> 00:02:48,350 And of course, after I'm done with that, Lizardo from s.M t.P Library 29 00:02:50,600 --> 00:02:58,690 import s.m t.p ssl us assim t.p. 30 00:02:59,120 --> 00:03:15,020 OK and then Lizardo from email dot mym dot text import my text. 31 00:03:15,230 --> 00:03:19,280 OK, so basically this invokes these two lines. 32 00:03:19,580 --> 00:03:25,690 Invoke the secure SMTP protocol which is on import 465. 33 00:03:26,000 --> 00:03:33,140 It uses the SSL server OK was already without the next lines of code. 34 00:03:33,140 --> 00:03:39,650 I would like to write them with an exception so we can ensure that if an error occurs we can encounter 35 00:03:39,650 --> 00:03:39,830 it. 36 00:03:40,160 --> 00:03:55,760 So let's do here, try and get a message for message equals to my text and then let's write a message. 37 00:03:55,760 --> 00:03:58,880 One say hi. 38 00:04:00,470 --> 00:04:02,750 OK then I'll write here 39 00:04:05,360 --> 00:04:07,010 plain OK. 40 00:04:08,750 --> 00:04:16,760 And then let's write m g which is our message and then let's write here a subject. 41 00:04:17,330 --> 00:04:26,800 OK, so bear in mind that the M is G is no object from the main text method. 42 00:04:27,050 --> 00:04:34,320 So here with a text middle to parse this parameters and we are outputting the created message. 43 00:04:34,460 --> 00:04:37,970 OK, so subject is equal. 44 00:04:39,260 --> 00:04:47,540 Uh message for Martin y ok. 45 00:04:48,380 --> 00:04:51,740 And then the right Asaji. 46 00:04:54,310 --> 00:05:08,590 Amadeu, and then from and then let's write this email p y Yanov, which is actually the name of my 47 00:05:08,590 --> 00:05:14,490 first work with Chris Mayer, with his name at Gmail dot com. 48 00:05:14,500 --> 00:05:17,070 So let's say he wants to send me an email. 49 00:05:18,100 --> 00:05:20,100 This is what we are going to simulate here. 50 00:05:20,590 --> 00:05:23,890 So this will be the from users or from this user. 51 00:05:23,900 --> 00:05:25,870 We are going to send an email message. 52 00:05:26,020 --> 00:05:30,400 Now, the next thing I will do is to create the assembly position. 53 00:05:30,850 --> 00:05:34,690 And basically the assembly session is quite easy to do. 54 00:05:34,990 --> 00:05:38,200 You basically need only one line you simply need to write. 55 00:05:38,200 --> 00:05:45,940 A simple tip equals s.m t p library. 56 00:05:46,150 --> 00:05:53,810 OK, DOT, SMTP and SMTP is a class from this A.P. library. 57 00:05:54,130 --> 00:06:00,410 So here we create an STPI object which will generate the assembly position. 58 00:06:00,880 --> 00:06:08,980 OK, so as we talked in our introduction to videos now here, since she declared there SMTP, you need 59 00:06:08,980 --> 00:06:12,520 to define the server that you want to connect. 60 00:06:12,820 --> 00:06:17,770 If you remember the graph from the previous video, you need to connect the server. 61 00:06:18,040 --> 00:06:24,640 So our server is going to be SMTP, dot Gmail dot com. 62 00:06:25,310 --> 00:06:31,270 OK, and then we're going to call the server on port five hundred eighty seven. 63 00:06:33,320 --> 00:06:34,780 For instance, OK. 64 00:06:37,520 --> 00:06:45,550 Now, one thing that is not necessary to do, but I always do it in order to ensure that everything 65 00:06:45,560 --> 00:06:48,140 is OK with the court, is that the bugging? 66 00:06:48,440 --> 00:06:58,670 So the S&P actually has a quite nice function and you can simply write s.m, keep dot set and then underscore 67 00:06:59,150 --> 00:07:02,860 and then Tiberg level, OK? 68 00:07:03,170 --> 00:07:05,090 And you can simply say just to true. 69 00:07:06,170 --> 00:07:10,190 So this means that our method will be completely the book. 70 00:07:10,340 --> 00:07:15,860 And finally, I will do the same t.p dot eho. 71 00:07:15,980 --> 00:07:23,590 And this is usually done in order to identify ourselves as Jhilmil client. 72 00:07:23,600 --> 00:07:29,870 OK, and for that reason, because we want to define ourselves as the client of the house that we are 73 00:07:29,870 --> 00:07:35,700 assessing for that reason we are going to the actual client. 74 00:07:35,800 --> 00:07:43,240 OK, once we're done with that, we're going to write a single line for encrypting the session. 75 00:07:43,520 --> 00:07:46,960 And this is quite easy and let's create a new flow. 76 00:07:47,330 --> 00:07:59,680 So if SMPTE p dot extern for external start here was OK, and if you're wondering what is the start 77 00:07:59,720 --> 00:08:02,070 of this, basically there's a twist. 78 00:08:02,210 --> 00:08:04,220 So let me actually write here. 79 00:08:04,730 --> 00:08:05,630 The T. 80 00:08:06,110 --> 00:08:12,620 S is a is encryption protocol. 81 00:08:12,680 --> 00:08:18,350 OK, so here we're going to check if we're able to encrypt the message. 82 00:08:18,360 --> 00:08:24,380 So if we have the encryption protocol, we would actually prefer to encrypted because it's more safe. 83 00:08:24,650 --> 00:08:31,100 So you can write a SMTP dot start to W and that's it. 84 00:08:31,280 --> 00:08:39,530 Here you are simply securing your email with the worst encryption and deal with encryption is basically 85 00:08:39,740 --> 00:08:41,620 similar to any other encryption. 86 00:08:41,630 --> 00:08:49,790 It simply gets your message, it generates a key and generates a main source so you cannot open it and 87 00:08:49,790 --> 00:08:50,750 you have the key. 88 00:08:51,290 --> 00:08:57,290 And once the email has been sent, this basically unlocks the encrypted message. 89 00:08:57,530 --> 00:09:00,610 So on the way there, just nobody can access it. 90 00:09:00,650 --> 00:09:01,070 OK. 91 00:09:01,430 --> 00:09:08,950 And after that, we would need to identify what was sent after the encryption. 92 00:09:09,170 --> 00:09:14,150 So we are doing that in order to make sure that the message that we received is the same as the one 93 00:09:14,150 --> 00:09:16,140 we send it after the encryption. 94 00:09:16,340 --> 00:09:24,380 So if red SMTP dot wall, this will actually check the message. 95 00:09:24,380 --> 00:09:27,840 Once it is, send it in after the encryption. 96 00:09:27,980 --> 00:09:34,190 Now, on the same the tension level as the IF statement, I am actually going to try another thing. 97 00:09:34,200 --> 00:09:39,850 So let's try and our block and let's add some space here so we can distinguish it. 98 00:09:40,190 --> 00:09:47,570 So let's try and then SMPTE p dot login in here. 99 00:09:47,570 --> 00:09:53,180 Guys, with the login function here in the brackets, you need to provide the user name and password, 100 00:09:53,450 --> 00:09:55,730 which means your email address. 101 00:09:55,730 --> 00:10:08,100 For example, here, my email address is P y Yanov at Gmail dot com and after that in parenthesis provide 102 00:10:08,100 --> 00:10:08,750 the password. 103 00:10:08,780 --> 00:10:17,360 Obviously I'm not going to show you the password of my email, but once you do this for yourself in 104 00:10:17,360 --> 00:10:22,230 that field, just add your password as it is just right in there, OK? 105 00:10:22,730 --> 00:10:27,280 So in the log in credit, those two can have your email in your password. 106 00:10:27,500 --> 00:10:32,200 So once you're on the code, your email is going to be assessed simultaneously. 107 00:10:32,660 --> 00:10:34,280 So for now, I will not allow it. 108 00:10:34,550 --> 00:10:36,410 I want it later when they run the code. 109 00:10:36,410 --> 00:10:42,190 So don't you worry, this code will run, but when you're creating it simply out there, your password. 110 00:10:42,620 --> 00:10:55,010 So we're trying this and if it doesn't work, we can do except and we are going to write a SMTP library 111 00:10:55,850 --> 00:11:05,210 dot s.m extension as E and here we can print. 112 00:11:05,570 --> 00:11:17,570 So this means if we fail to login, we'll just say authentication failed or indication error. 113 00:11:18,560 --> 00:11:19,310 That's right. 114 00:11:19,870 --> 00:11:22,700 Colma e right. 115 00:11:23,090 --> 00:11:28,530 And after that we will write sis dot exit one. 116 00:11:29,570 --> 00:11:35,170 So this means guys, if we have a problem with our creditors, we are going to receive the right error. 117 00:11:35,240 --> 00:11:35,740 So the. 118 00:11:35,980 --> 00:11:42,300 We know exactly what happened, so let's quit on our blog, so let's do try. 119 00:11:43,490 --> 00:11:47,390 OK, and here we're basically going to send the email. 120 00:11:47,650 --> 00:11:58,000 So until now, we actually connected to the server here and then we encrypted the message was that the 121 00:11:58,030 --> 00:12:03,590 encryption protocol, then we added our credit those. 122 00:12:03,820 --> 00:12:05,770 So right here. 123 00:12:07,210 --> 00:12:12,880 So you're actually aware adding created those. 124 00:12:14,970 --> 00:12:15,520 OK. 125 00:12:16,080 --> 00:12:24,530 And after that, uh, after we pass this section, we're now going to send the email, sending the email. 126 00:12:24,730 --> 00:12:30,560 OK, and this right here is SMTP dot sendmail. 127 00:12:32,190 --> 00:12:39,330 And then what you need to do here is to write the email from which you're going to send the message, 128 00:12:39,630 --> 00:12:45,060 which is pure white Yanov at Gmail dot com. 129 00:12:46,140 --> 00:12:51,900 OK, then in the brackets, write the email to which you want to send the email list. 130 00:12:51,900 --> 00:12:57,180 Right, MPLX Yanov at Gmail dot com. 131 00:12:57,580 --> 00:13:04,760 OK, which is my email and then am S.G. Dot. 132 00:13:05,850 --> 00:13:14,250 So images and messages that were created earlier and then US string and that's it. 133 00:13:14,250 --> 00:13:15,620 We're ready here on this line. 134 00:13:15,840 --> 00:13:19,680 So if you wonder what is the image, you will be created right here. 135 00:13:19,680 --> 00:13:27,880 And this is basically our message, which has a topic in which he has a sender and which has a text 136 00:13:27,900 --> 00:13:28,580 inside. 137 00:13:29,670 --> 00:13:31,620 OK, so we simply refer to it. 138 00:13:32,370 --> 00:13:42,510 And now if this don't work, so if we fail to actually send the message, we are going to write except 139 00:13:42,990 --> 00:13:51,990 and then socket dot gate error then socket. 140 00:13:53,380 --> 00:14:03,730 That error, and then we're going to create another circuit age error and then we're going to write 141 00:14:03,740 --> 00:14:10,240 A.S.A.P. Library DOT is empty B exception. 142 00:14:11,680 --> 00:14:12,240 Mm hmm. 143 00:14:12,880 --> 00:14:19,780 So this is how we're preventing ourselves from that errors. 144 00:14:19,810 --> 00:14:24,810 So this is going to be us e and let's try it on the next row. 145 00:14:25,060 --> 00:14:31,410 So this is supposed to be the exception in the next rule we can write print. 146 00:14:32,380 --> 00:14:40,750 So if we get exceptional print and then write your message. 147 00:14:42,190 --> 00:14:46,210 He has not been sent. 148 00:14:47,280 --> 00:14:59,270 OK, so you get this message, if your message does not send it in some way, then will print he OK. 149 00:15:00,400 --> 00:15:07,300 So we're going to print all the errors and then insist that exit and the result will be one. 150 00:15:08,080 --> 00:15:15,610 And finally, we can write finally and then quit. 151 00:15:15,610 --> 00:15:19,900 Actually, SMPTE people need to quit the protocol and then quit. 152 00:15:20,330 --> 00:15:20,900 That's it. 153 00:15:21,520 --> 00:15:22,940 So we're ready here. 154 00:15:22,970 --> 00:15:28,720 Guys, this is how we're sending the email and how we're preventing it from unexpected errors. 155 00:15:30,190 --> 00:15:34,290 So finally, let's finish by exception. 156 00:15:34,310 --> 00:15:43,020 So I write except and if you can see here, this whole block of code is also a trite statement. 157 00:15:43,030 --> 00:15:49,270 So I will need to do something similar as this, except look here, the previous one. 158 00:15:49,480 --> 00:16:06,310 So I will do socket dot dot gate error and then socket dot error, then socket Donald H error and then 159 00:16:06,700 --> 00:16:08,980 s.m type library. 160 00:16:09,190 --> 00:16:18,990 Does it seem to be extension that set us e OK, perfect. 161 00:16:19,390 --> 00:16:27,900 And finally, let's try to print and in the print statement our basic right to send a message. 162 00:16:27,910 --> 00:16:36,310 So here we are going to write an error or what's going to appear if the try block earlier on don't work. 163 00:16:36,610 --> 00:16:44,180 So C message has not been sent. 164 00:16:45,520 --> 00:16:47,920 OK, we are ready here. 165 00:16:47,930 --> 00:16:53,650 Let's write some exclamation marks then let's print E! 166 00:16:55,570 --> 00:17:03,970 Sorry, and then we will exit on one sees that exit and then one. 167 00:17:04,480 --> 00:17:07,020 OK, so guys, we're ready with the code. 168 00:17:08,020 --> 00:17:09,250 This was the whole code. 169 00:17:09,580 --> 00:17:12,060 Let me show it to how long it is exactly. 170 00:17:12,430 --> 00:17:14,760 So you can see this is the whole code here. 171 00:17:15,730 --> 00:17:19,720 And now let's go to the terminal where I will show you how to run it. 172 00:17:20,200 --> 00:17:23,350 So here I am in the terminal in the resources folder. 173 00:17:23,590 --> 00:17:32,800 So to see the Section five airways and you can see that we have only the centipede underscored G.M. 174 00:17:32,800 --> 00:17:33,550 protocol. 175 00:17:34,600 --> 00:17:36,090 So as we're as Section five. 176 00:17:36,100 --> 00:17:36,600 That's right. 177 00:17:36,620 --> 00:17:42,770 Python, Python and then jumbled dot, pure white and let's see if it works. 178 00:17:43,480 --> 00:17:47,020 So actually, on to here, we're getting the connections. 179 00:17:47,440 --> 00:17:50,680 But there was some error on line 18. 180 00:17:50,760 --> 00:17:53,800 It is SMTP object has no attribute. 181 00:17:55,120 --> 00:17:56,620 So let's go back to the code. 182 00:17:57,190 --> 00:18:06,790 And actually, yes, here is not here, but instead it is has exten, as you can see here, it's now 183 00:18:06,790 --> 00:18:07,540 predefined. 184 00:18:07,540 --> 00:18:10,600 So I remove the previous one and I will save it. 185 00:18:10,960 --> 00:18:13,580 Let's run the code once again and see what happens. 186 00:18:13,620 --> 00:18:20,110 So I write Python is empty B at Gmail dot com or Gmail dot pure white. 187 00:18:20,720 --> 00:18:27,730 OK, we're getting no authentication error and actually does not surprise because I don't yet added 188 00:18:27,730 --> 00:18:28,810 my password here. 189 00:18:29,140 --> 00:18:36,800 So let me add my password from another terminal or another tab and then I will try it again. 190 00:18:37,120 --> 00:18:43,360 So now I added my personal password and let's run the code again and see if we're going to face any 191 00:18:43,360 --> 00:18:45,100 other errors that we'll need to fix. 192 00:18:45,400 --> 00:18:52,060 Xiaodong the word if we are facing errors, this is actually quite good because we can see how easily 193 00:18:52,060 --> 00:18:58,900 we can debug them and you can kind of get the feeling that the errors are not something that bad because 194 00:18:58,900 --> 00:19:03,330 everything is quite descriptive and you can very easily fix it. 195 00:19:03,580 --> 00:19:05,700 So let's run the code again and see. 196 00:19:06,550 --> 00:19:16,060 So as you can see now, everything looks fine and you see the different communication messages here. 197 00:19:17,260 --> 00:19:19,330 You don't have any errors, right? 198 00:19:19,630 --> 00:19:25,240 So you can see the reply, the same status, the post that you're opening. 199 00:19:25,250 --> 00:19:31,570 So for example, here, the 220 is ready and 220 is the power that we are sending with. 200 00:19:32,050 --> 00:19:40,440 You can see that here we open the server connection because the SMTP, Gmail dot com is at your service. 201 00:19:40,780 --> 00:19:44,620 So it open the port and here we can see all the rest. 202 00:19:44,860 --> 00:19:48,100 But actually, there's only one proof if everything worked. 203 00:19:48,670 --> 00:19:51,460 So we as the only way is simply to check my email. 204 00:19:51,820 --> 00:20:03,340 So as you know, we stated, though, that the message should be sent from one P y Yaniv and dot com 205 00:20:03,340 --> 00:20:07,600 to my MP Yaniv at Gmail dot com. 206 00:20:07,930 --> 00:20:18,460 So actually, since the courts accepted by the system and I can see the message sexual send it, let's 207 00:20:18,460 --> 00:20:21,420 do it once again while my email is here. 208 00:20:21,880 --> 00:20:30,150 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 209 00:20:30,150 --> 00:20:30,720 P white. 210 00:20:30,730 --> 00:20:34,960 So let's try it and let's see if I'm going to receive the message at the same time. 211 00:20:35,590 --> 00:20:42,670 So I'm going to run the code and you can see that I received a new message from my Gmail immediately. 212 00:20:43,450 --> 00:20:51,130 And the message has a subject message from Martin White in the same way as you wrote it in our script. 213 00:20:51,550 --> 00:20:53,820 And you can actually see our script here. 214 00:20:54,100 --> 00:20:59,440 So the message for for Martin White was written here. 215 00:20:59,440 --> 00:21:02,890 So this is a subject and here's how White in the subject name. 216 00:21:03,130 --> 00:21:09,220 And then we have a message, one CIHI, which is actually what is inside the message. 217 00:21:09,460 --> 00:21:16,960 So this is a very easy way, guys, to send automated messages to multiple people in emails. 218 00:21:16,960 --> 00:21:21,010 If you desire, you simply establish the connection with the Gmail. 219 00:21:21,010 --> 00:21:25,930 And then it is very easy to send your message in the book for errors. 220 00:21:26,260 --> 00:21:29,590 So that was everything that they want to share with you for today. 221 00:21:29,590 --> 00:21:38,680 Guys, I hope you enjoyed it and you are able to actually send an email with this script. 222 00:21:39,610 --> 00:21:40,380 So that's it. 223 00:21:40,570 --> 00:21:46,150 Thank you very much for watching and bear with me in the next videos because we are going to do some 224 00:21:46,150 --> 00:21:48,610 more fun stuff with email protocols. 225 00:21:48,940 --> 00:21:49,870 Thanks for watching.