1 00:00:05,470 --> 00:00:12,370 Retrieving emails with IMAP clients, hi, guys, were already, as you can see in the terminal because 2 00:00:12,490 --> 00:00:17,440 they were going to use the IMAP protocol in order to retrieve some messages. 3 00:00:17,680 --> 00:00:22,630 First of all, again, I want to show you here in the emails. 4 00:00:23,700 --> 00:00:28,450 So in your Gmail, in SEO settings, select this field. 5 00:00:28,690 --> 00:00:34,050 And if you don't enable the email, please select forward to pop. 6 00:00:34,060 --> 00:00:40,600 I'm up and then go down and enable that I'm up, as you can see here, OK, and enable all of those 7 00:00:40,600 --> 00:00:41,050 options. 8 00:00:41,320 --> 00:00:47,500 But you will usually be there automatically once you select enable IMAP, because if you don't do anything, 9 00:00:47,770 --> 00:00:50,880 this button will be selected to disabled IMAP. 10 00:00:50,980 --> 00:00:57,340 OK, so I just want to do this because if you don't do it otherwise, our code will not work because 11 00:00:57,340 --> 00:01:01,480 obviously to use IMAP on your Gmail you need to have it enabled. 12 00:01:01,810 --> 00:01:02,290 OK. 13 00:01:03,730 --> 00:01:11,970 Set up here are in our terminal and I will actually open the pie chart here and I will create a new 14 00:01:11,970 --> 00:01:15,170 file there so we can implement some functions. 15 00:01:15,570 --> 00:01:21,380 So the first file that we are going to create, let's create a new python file. 16 00:01:21,720 --> 00:01:26,040 And here I will write folder in full. 17 00:01:26,190 --> 00:01:29,490 OK, so here, here the file, guys. 18 00:01:29,670 --> 00:01:34,680 But before writing, let's go to a terminal, because you need to make sure that you have installed 19 00:01:34,680 --> 00:01:36,270 the I am up protocol. 20 00:01:36,320 --> 00:01:39,000 OK, you can definitely do that with the pipe. 21 00:01:39,000 --> 00:01:39,510 So right. 22 00:01:39,510 --> 00:01:43,980 Pipe install and then I up client. 23 00:01:44,190 --> 00:01:52,440 OK, so install this and you can see that I already have this package, but for you it might take some 24 00:01:52,440 --> 00:01:54,500 time to install was don't have it yet. 25 00:01:54,900 --> 00:01:58,650 So then let's go back to the pie chart and let's start writing. 26 00:01:59,070 --> 00:02:00,480 So let's write here. 27 00:02:01,580 --> 00:02:07,500 User slash bean slash N.V.. 28 00:02:07,800 --> 00:02:12,600 OK, and then let's write Python three, OK. 29 00:02:12,600 --> 00:02:22,640 And then I'll write from I am up client import IMAP client. 30 00:02:22,680 --> 00:02:28,610 OK, so this will be our class from which we are going to create a map object. 31 00:02:29,160 --> 00:02:36,910 OK, so after that let's also import the get sorry. 32 00:02:37,110 --> 00:02:37,950 Get past. 33 00:02:38,010 --> 00:02:42,170 OK, and this will be used for us to create a secure password. 34 00:02:42,570 --> 00:02:48,600 The first thing to do is to ask the user to create his username and password like we always did. 35 00:02:48,850 --> 00:02:49,770 So let's try it. 36 00:02:49,770 --> 00:03:04,260 User name equals input, ok, input and then enter your user name. 37 00:03:05,170 --> 00:03:11,880 OK, and after that let's write password in here. 38 00:03:11,880 --> 00:03:21,420 I will do get pass that get passed and here and said let's write prompt equals. 39 00:03:21,690 --> 00:03:25,560 And the prompt is basically what the user will see before writing his password. 40 00:03:25,560 --> 00:03:30,610 So the prompt will be enter your password. 41 00:03:31,350 --> 00:03:33,660 OK, let's add some space here. 42 00:03:33,810 --> 00:03:38,500 Actually I will do it without space because you are not going to see your password anyways. 43 00:03:39,540 --> 00:03:40,410 Let's try this now. 44 00:03:40,410 --> 00:03:42,390 Server, SQL Server. 45 00:03:42,870 --> 00:03:48,170 And here here is how we'll be connecting to the server actually mixed in the space here a bit. 46 00:03:48,780 --> 00:03:55,560 So here, when you write a server, let's write, I'm a client, OK? 47 00:03:56,460 --> 00:04:02,850 And then I map that Gmail dot com, OK. 48 00:04:03,450 --> 00:04:08,850 And then comma SSL equals two through OK. 49 00:04:09,690 --> 00:04:13,050 And after that let's log into the server. 50 00:04:13,050 --> 00:04:18,420 So let's write server dot login. 51 00:04:18,810 --> 00:04:25,650 So let me do that in the first line here, we're basically creating a variable that is a type I'm up. 52 00:04:25,650 --> 00:04:31,440 So here we simply connect to the server and pass all the server credits to the server here. 53 00:04:31,620 --> 00:04:40,470 And in the next line we're going to connect to our email on this server by writing our username and 54 00:04:40,470 --> 00:04:42,720 password so you don't need any other things there. 55 00:04:43,110 --> 00:04:49,140 Now, let's write a variable select info. 56 00:04:49,860 --> 00:04:57,150 And here we're going to write one line with which we're going to get the info from our mailbox. 57 00:04:57,360 --> 00:05:02,280 So we'll write a server dot select. 58 00:05:04,120 --> 00:05:15,190 Folder, comma, and then they write in box to get the mail from the inbox, read only so will not be 59 00:05:15,190 --> 00:05:17,620 able to update and they will right through. 60 00:05:17,800 --> 00:05:20,950 OK, so this is how we read our mail. 61 00:05:20,950 --> 00:05:33,060 And then let's write for K and V in list and then select info dot items. 62 00:05:34,350 --> 00:05:37,590 So for each item in this list we would like to print it. 63 00:05:37,960 --> 00:05:39,920 So let's print it. 64 00:05:39,940 --> 00:05:48,670 This is basically we're going to print information from our mailbox so percent s and then percent are 65 00:05:48,880 --> 00:05:49,470 OK. 66 00:05:49,720 --> 00:05:59,180 And here in the brackets I will write K and V, which are the parameters that we're going to print. 67 00:05:59,200 --> 00:06:01,470 And now finally, we need to close the connection. 68 00:06:01,480 --> 00:06:04,950 So let's write server thought. 69 00:06:04,960 --> 00:06:08,120 Log out, log out and that's it. 70 00:06:08,590 --> 00:06:09,370 So we are ready. 71 00:06:09,760 --> 00:06:16,270 So since are really guys, I saved the file and let's go back here to the terminal and let's write aways 72 00:06:16,540 --> 00:06:21,360 and you can see that we already have the folder in full file right here. 73 00:06:21,700 --> 00:06:28,570 So let's write Python and then folder in full and now we will need to enter your username. 74 00:06:29,110 --> 00:06:30,730 OK, and then the password. 75 00:06:30,880 --> 00:06:38,170 OK, so now we're connecting to my mailbox and you can see that this was quite fast and you can get 76 00:06:38,170 --> 00:06:44,890 all the information from the folders in my mailbox, something that definitely you cannot do with the 77 00:06:45,340 --> 00:06:46,480 proper protocol. 78 00:06:46,720 --> 00:06:53,230 And you can see that there are existing one hundred and twenty one emails we are getting here, the 79 00:06:53,230 --> 00:06:59,800 answer to the flag, the drafts, the deleted scene, not phishing and phishing emails. 80 00:07:00,070 --> 00:07:04,950 So basically you are getting all the information for your email that you need. 81 00:07:04,960 --> 00:07:06,850 So we don't have a recent email. 82 00:07:07,430 --> 00:07:12,830 We have the text in the the validity and so on. 83 00:07:13,120 --> 00:07:19,910 So here is how you can basically get the information for your email quite easily using the IMAP protocol. 84 00:07:20,050 --> 00:07:26,400 Now, all this if I was working, let's actually create one more file and the next file will be called 85 00:07:26,680 --> 00:07:28,240 mailbox listing. 86 00:07:28,420 --> 00:07:35,260 So let's create new python file and this will be called mailbox listing. 87 00:07:35,260 --> 00:07:42,250 And we're going to see all the capabilities that our Gmail can do with the IMAP using this file. 88 00:07:42,640 --> 00:07:44,770 So let's try it here again. 89 00:07:45,190 --> 00:07:56,410 Let me get closer and let's right now user slash bin slash Ian fee and then Python three. 90 00:07:56,630 --> 00:08:01,780 OK, and then let's import OK, import prices. 91 00:08:02,620 --> 00:08:09,130 And then from a map client. 92 00:08:09,430 --> 00:08:11,680 I am of Klamt import. 93 00:08:12,280 --> 00:08:15,250 I am map client. 94 00:08:15,530 --> 00:08:18,190 OK, so we have them up client here enabled. 95 00:08:18,400 --> 00:08:27,040 And of course I'm going to import the guest password toolkit so we can add the password and you can 96 00:08:27,040 --> 00:08:27,780 do the same thing. 97 00:08:27,790 --> 00:08:40,410 Let's write user name equals input and then they input it will be enter your username. 98 00:08:41,620 --> 00:08:57,010 OK, and after that I will write password equals get pass and here I will write dots and get pass in 99 00:08:57,010 --> 00:08:57,880 the brackets. 100 00:08:58,450 --> 00:09:07,210 Let's write prompt equals to enter your password. 101 00:09:08,940 --> 00:09:18,000 OK, next list are other server as the same as we did last time, so server will be equal to IMAP client 102 00:09:18,630 --> 00:09:25,150 and in the brackets let's write IMAP dots and Gmail dot com. 103 00:09:25,470 --> 00:09:31,710 So in that way we're connecting to the Gmail and then SSL equals two through. 104 00:09:32,160 --> 00:09:37,380 OK, and then we're going to try here a statement. 105 00:09:37,380 --> 00:09:44,850 So we're going to fight server dot login username and password. 106 00:09:45,510 --> 00:09:51,570 So we're creating a triangle here guys, because we want to be sure that we assess the email correctly. 107 00:09:51,570 --> 00:09:55,160 And if not, we would like to display the proper error. 108 00:09:55,470 --> 00:10:03,030 So if we don't, we don't assess it in the correct way, with one exception here and this will be server 109 00:10:03,030 --> 00:10:12,620 dot error us e and on this will print could not walk in. 110 00:10:12,900 --> 00:10:23,550 OK, and then Colma e so this method will be added to the error and then sis dot exit and exit with 111 00:10:23,550 --> 00:10:24,940 one or whatever. 112 00:10:26,010 --> 00:10:33,630 Now since we are ready with that, let's print and let's provide some user friendly information here. 113 00:10:33,870 --> 00:10:35,940 So let's print capabilities. 114 00:10:36,570 --> 00:10:44,130 OK, and here I will write Colma server dot capabilities. 115 00:10:44,430 --> 00:10:48,510 OK then let's print another thing. 116 00:10:48,510 --> 00:10:52,620 So here I will print listing mailbox. 117 00:10:52,750 --> 00:10:57,160 OK and then let's write. 118 00:10:57,160 --> 00:11:06,550 The data will be equal to server dot list underscore folders. 119 00:11:06,720 --> 00:11:19,200 OK, so here we have some list of our folders and then I will write for Flock's Delamater and folder 120 00:11:20,400 --> 00:11:29,280 name in data and the for the list folders is giving all this information. 121 00:11:29,560 --> 00:11:42,420 That way we can freely provide it here and here we are going to print percent minus 30 s percent s and 122 00:11:42,420 --> 00:11:48,200 then percent s and then here we are going to display the following. 123 00:11:48,210 --> 00:11:56,910 So empty space dot and join a string flux. 124 00:11:57,120 --> 00:11:57,720 OK. 125 00:12:00,150 --> 00:12:05,230 Then comma the meter and then folder name. 126 00:12:05,480 --> 00:12:08,460 OK, that's it. 127 00:12:08,490 --> 00:12:12,210 So once we're ready here, guys, you can close the server. 128 00:12:12,210 --> 00:12:17,970 So server dot logout and we're ready here. 129 00:12:18,270 --> 00:12:21,450 So this was a bit of a struggle, the last lines. 130 00:12:21,450 --> 00:12:26,300 But if you want somebody to look in a nice way, you need to a little bit more of a struggle. 131 00:12:26,640 --> 00:12:32,030 But basically, this is just the design is not what we are here about. 132 00:12:32,040 --> 00:12:32,270 Right. 133 00:12:32,580 --> 00:12:38,970 So if you don't understand fully the last line, don't worry, the main functionality is way above it. 134 00:12:39,650 --> 00:12:43,550 So let's save that file here and let's go to the terminal. 135 00:12:43,800 --> 00:12:45,440 So let's write a list here. 136 00:12:45,450 --> 00:12:50,220 And as you can see, guys, you can see the mailbox listing file. 137 00:12:50,230 --> 00:13:00,510 So let's write Python and then mailbox listing and now simply provide your username and your password. 138 00:13:01,110 --> 00:13:08,730 OK, so once you do that, you can see this funny looking message, but you can see here his children 139 00:13:09,060 --> 00:13:16,560 in box, which means that we have some folders over there and you can see basically wishfulness have 140 00:13:16,560 --> 00:13:21,320 children or folders or emails inside and which do not. 141 00:13:21,540 --> 00:13:28,710 And you can see basically the different types of folder like trash start spam sent mail important and 142 00:13:28,710 --> 00:13:29,160 so on. 143 00:13:29,370 --> 00:13:37,890 So all these folders that are already created in your Gmail guys are basically, um, there simply because 144 00:13:37,890 --> 00:13:44,040 of the IMAP protocol and it enables your emails to build automatically stored in this type of folders. 145 00:13:44,220 --> 00:13:50,760 And above that you can see the capabilities which are the different ideas and functions that you can 146 00:13:50,760 --> 00:13:53,320 basically do with the emails in your mailboxes. 147 00:13:53,670 --> 00:13:57,690 So this is the way that you can use a map in order to display the folders, guys. 148 00:13:57,930 --> 00:14:03,990 And that's, I think, very much for watching this video and actually this section, because it's the 149 00:14:03,990 --> 00:14:07,110 last course in the last video in this section. 150 00:14:08,040 --> 00:14:09,810 Thanks for watching that section, guys. 151 00:14:10,050 --> 00:14:12,570 And I will see you in our next adventure.