 Right, so hey guys and welcome back to another Python tutorial So as you guys may have known in the last tutorial I showed you guys how to create a basic GUI application using which you could basically send emails to a single recipient So pretty basic stuff and it was intended for kind of beginners to like intermediate people So first of all, I'd like to thank you guys for all the support you guys have shown on the previous video And if you guys could keep sharing it that would be amazing So for the people that are new to this tutorial I would highly recommend going ahead and watching the previous video Which will be linked in the description top links in the description Because this whole program that I'm about to do today, which is about attaching files to the email is all going to be linked so First off, I'm going to show you guys what we did in the last tutorial So if I don't click on my program I'll be able to show you what we caught up to in the last tutorial So we had this little GUI application going where we're going to enter our own email our own password The recipient's email a subject and a body and we were able to send this email and then after just reset the fields That's all we did in the last tutorial. So we're going to add on to this GUI application we've got going here and we're going to add an attachment button Which is going to allow us to pretty much attach files So this video is going to be split into two parts one part two is going to be released a bit later In the first part, I'm going to show you guys how to attach any type of files into the email and then send it off and in the second tutorial I'm going to show you how to attach multiple files and then send it to a Given email so in this tutorial you will still be learning how to attach files But you're going to be able to only attach one file at a time. So without further ado, let's begin First off, I'm going to open the file that we created last time and we're going to look at what we did in the last tutorial So we had this code going in the last tutorial First off what we need to do is import something else that we're going to need. So we're going to need something called An email message, which is sort of like an object that's going to help us Reduce some of the code and make it a bit more easier to understand So we're going to have to edit some of the previous code that we had in order to create attachments quite easily so first off what I'm going to do is go ahead and Import what I need so from email.message import capital E Emo capital M message Right, so I'm going to quickly run this program to see if the import was success and as you see it was So what this right here is going to do is it's going to allow us to pretty much get rid of this This little formatting we've got going on here. So that's going to be quite fun and interesting to deal with. So first off what I'm going to do is I'm also going to import the file dialogue from the tick into So let's hold on with this for a moment And I'm going to do the other import because we're going to need a file dialogue What that does is basically allows the user with a graphical user interface where the user can select a file and Then once the user selects a file so something like this when I click on file open file, it has a little pop-up window This is what this Module is going to let us do when we import it It's going to let us select a file and when we select the file it's going to return the location Including the file name, which is what going to which is what we're going to be needing to attach the file So I'm going to type in from to Kinta File dialogue, which is what we're going to need I'm going to cut this and paste it below to Kinta just to make a bit more sense out of it So this is the bit we're going to be needing for to Kinta to work out so that the user can select a file and this is the bit that's going to help us organize and Make our code a bit more neat and actually allow us to attach the files. So first off what I'm going to do is go ahead and Create in this tri-section right here just before the username. So under the send function Create a new line and then create a new variable called message MSG and then I'm going to equals that message I like to keep it invented here. It's kind of OCD so MSG equals email message Make sure you spell it correctly with a capital E and an M. So that's initializing an object Now this is like a little object that we can assign information to Which is why instead of passing through So when we send an email in this line over here where it says server.sendmail We have to include the username the to and the final message So with message with this what we could with this message object What we could do is we could attach all those details in there already and then finally just attach the message variable by itself So I'm being a bit confusing. What do I mean by that? So let's go ahead and show you what I mean by that. So first off. I need to assign the subject the body the subject and the from and The to to the message object. So let's go ahead and do that So what I'm going to do is go down here and type in MSG subject So this is kind of the property that I'm giving to my object equals subject msg What's the other field we're going to need is from equals It's not from for us. It's going to be username because we've stored it in there and then finally msg to Try not to change these names because they might give you errors equals To because that's the same and now finally to attach our body. We don't need to do msg Body we have a special keyword or a function for this which is msg.sets Content and then we add body in there So now we're pretty simply attached everything that we need Which is the subject the username the to and the actual body of the email to this message object right here So now what we need to go ahead and do is delete this final message variable or this line right here We're going to delete that off because we're not going to need it anymore and Then instead of doing server dot send mail We need to change this line to server dot send message because now we're going to be going ahead and sending a message Which is why we need to kind of change the method which we were using previously. So we're going to do Server dot send Message message right, so that's what we're gonna have to do over there and What that's gonna allow us to do is actually send the email through so Let's go ahead and run this program now to see if our emails are still going through So this is still plain emails. Let's take a look if they're still going through with this new method We've just implemented click on run and this is quite important to do because hey one best practice And also it's gonna let us actually attach the files if we don't use this We're gonna have to take a very long route to attach files, which I don't want to Go hard on you guys Hey and without failure it seems like it has worked Let's take a look in my actual email of the mode of show it up Hold on a second. Let me take a look Let's take a look and Without a doubt it says an email form terminal for with a subject and a body of test So as you guys see, this is a lot more easier to actually Set and understand instead of using the previous line that we were using to like format and all that So that's part one of this done, which is implementing a better solution to actually sending the plain email Now what we want to do is we want to go ahead and Create a new button on our GUI application. That's going to be called attachments So when the user clicks on this button the file dollar dialogues needs to pop up So let's go to a button section right here as we've commented it And then I'm gonna go ahead and just gonna copy the previous one because I don't want to waste your time paste and then I'm gonna change this to change the text to Attachments so this tutorial is just attach meant So I'm gonna leave it to attach meant because obviously in the next tutorial, which is part two of this I'm going to show you guys how to attach multiple files. So the command is going to be changed to Attach well so attach file is going to be the command and we're going to be creating this function very soon and Then the we're gonna have to change I'm gonna have to change the padding x to 50 60 75 I'm assuming and Let's run this to check if it's working Attach power is not defined. So let's quickly create dummy function up here because otherwise it's going to keep annoying us Attach Okay, I don't even care that I've spelled it wrong. It's fine. So Okay, so it looks like we needed to leave a bit more space there. Let's take a look I was here so Let's go with 85 Okay, that seems fine So we've got now our three buttons. Actually, it's bugging me that there's a bit of space there. So try 90 All right, cool. So we've got three buttons now send resets And attachments the attachment button is essentially meant to bring us that pop up window was talking about the file dialogue Where the user can select the file and that file will get attached And then there'll be like a little text down here label saying How many files are attached? They're not going to go too complex on that So I'm going to close this and let's start coding the attach file function So in here the basic concept of this attach file function is To actually just open up the pop-up window and then whatever files the user selects. Obviously, we're going to be returned with the file name What do I mean by that? Let me go ahead and show you So if I do create a new variable so file name equals And this is how you get the file dialogue to show up. So file Dialogue and if you remember we've imported it up here. It's from tecinta So it's not going to have any issues if you've imported it correct dot ask open file name file name It's kind of a long name for a function, but hey, it's fine And then we need to initialize a few things. So there's a few properties that you can set you can if you don't want to It's fine, but I'm going to go ahead and do it. So initial Dur or directory you can set that up to whichever directory you want this pop up to open it So I want it to open in my c drive for now And then I'm going to go ahead and add a comma And then title equals Select a file So that's going to be the title of the window that pops up later So that's that and I'm going to quickly run this to show you what happens when I run this So I've run this and attachments You see the title that we set up is showing up right here select a file and then this um File explorer window or this file dialogue window has opened up right in our c directory, which is what we wanted I'm going to close this off And now if I show you what it returns so print File name it should return me the destination including the file name Of whatever file I select so attachments and then let's say I go to my desktop I've already got demonstration purpose file in here website dot jpg Double click and once I double click if you notice what it does is it gives me the exact path to my file Including the file name and the extension, which is perfect So what we're going to do with these file names that we're gathering is store them in an array So that we can actually go ahead and read those files and then attach them to the email and send them off So first off go ahead into your main screen in it or like right under the imports and then create a section for Global variables, okay, and then I'm going to create a new variable called Attach And then I'm going to assign that to an array which I said to you guys before Whatever file name we find in here or whatever file name the username to the user chooses We're going to be pushing that or adding appending that to this array. So it's going to be added to the array So to add that to that array. We very simply just type in attachments dot append File name And now we also need to notify the user saying the file just got touched So notif.config if you guys haven't watched the previous tutorial, this is why it would be really helpful Because we kind of went through these conventions before And for you guys that have already watched it. So you should be able to understand This is just a notification label that we're using to display some text now So foreground equals green because it's a friendly message Go ahead and change it to however you fancy, but I'm going to go with this one for now text equals attached plus str Length so lend what lend does is it gives us the length or the How many items are going to be in this array? So how many file names are going to be in this array? So attached As many file names that are in the array and then we're going to convert it to a string because we're concatenating plus um files We're going to leave a little space in here because otherwise it's going to look real ugly. We don't want that to happen to me So we're going to change this to a bit of space as well quick run to see what we have to So attach a file. Okay, great file name is not defined If Right, that's Where did the previous line of code go? Okay, anyway I might have messed it up and lost my previous line of code somehow It seems like so i'm going to quickly type that up people Dialogue Ask open Funny Image Um Title equals please select a file Cool, so let's take a look at that now. That was weird. I might have erased it actually while I was doing this stuff So attachments go to my desktop and then click on this website So it says attached one file now I'm going to go attachments again and then click on one of the dummy files It says attach two files and as you guys can see that's going to keep updating as we keep adding stuff to our array So if you guys are um following on so far And now we're going to go ahead and do some of the main bits also realize that we need a bit of space Okay, cool. So that's the attach file functions done Which all it does is it opens a pop window which the user can use to select the file As soon as the user selects a file we have a file name with the directory that's stored inside our variable Then we go ahead and append that to our array And then we create a little notification saying how many files have been attached or how many files that are in the array at the moment So great, that's done now We're going to go ahead and move on to kind of the final phase of this tutorial Which is going to be like the most confusing I guess but if we um if you guys follow along that's great So last bit what we need to do is actually attach this file into the email So to do this the only issue is that um while sending image files, there's a different protocol compared to While sending other files such as pdf I don't know any other document files Or py files, for example, they have different protocols So we got to follow those protocols, which is why we need to differ We need to find out what the extension of the file is So if the extension is something like jpeg jpg or png or any other image file We've got to use the image protocol to send the image or attach the image But if the extension is not an image file, then we've got to use the standard one to send it as a document So how are we going to do that? Let's go ahead and take a look So we go in here in the try section right here in the send function And then as as soon as you've done the message dot set We're going to have to After that we're going to have to do a new variable called file name And we're going to have to assign that to attachments zero So what this is going to do is it's going to create a new variable called file name And it's going to set that to the first um first file directory or the first Uh file that the user selected as his or her attachment, which is what I said You can only do one in this tutorial So Once you're done with that we've got to determine the type of file type it is so that we can actually distinguish Um between what protocol to use so file type equals um file name dot split Dot so what this does is anything after the dot that's in this um File name it's going to split it and give it to us into an array It's going to split it into like two halves if I do something like print File type it's going to return an array to me with the file name Uh including the directory and the extension of the file, which is going to be separate Let's take a look at what I'm on about run this quickly And actually I'm going to move this bit. I'm just going to Make a quick copy and then paste it in here since we're just we're not going to be sending anymore for now We're just going to be showing you guys what it does So I'm just going to indent this quickly Run it and now if I do attachments Website dot jpeg Now if you see right here as I said before it returns an array to me Where it gives me the location And the file name as well as the extension by itself So the file type we're going to have to target the one so location of the array Looking for is one because at zero we have the location And at one we have the extension which is why we're going to be using one so We're going to update the file type so file type is going to be overwritten and we're going to say file type One so what that does is it's going to give us only the extension so png jpeg or whatever that is So now we add our f same. So here file type equals jpg Or file type equals Uh capital jpg because sometimes that works as well or file types equals png or file type equals Png you guys get the idea So you guys can go ahead and add all the conditions for the image files that you might actually add or the possible ones But for this tutorial, I'm only going to do jpegs and pngs Now in here What i'm going to say is for now we're just going to print image detected and then else print Uh document detected So this is what i was on about so we need to kind of distinguish between an image So that we can use the image protocol to attack the image and a document so that we can use the document protocol to attach the document Run this quickly just to show you what's going on Um Actually, we yeah, so i'm going to run this quickly attachments Select website and it says image detected Now if I go back to the gooey and select attachments again and select this random file it's Okay, that's um it says image detected because as I said we can only attach one image one file at a time so if I run this again and then attachments Select this it says document detected which is perfect Now since I was just showing you guys and this was all for testing purposes We're going to actually delete this code because it doesn't belong in attach files. It belongs in uh It belongs in What is called send because we need to do all of this stuff in the send Now i'm going to just paste this right here Okay Python's great when it comes to indentation, isn't it? Okay, great. So we've got a file name Then we've got a file type and then we've overwritten it with the actual file type Then we're checking if it's an image by checking for jpegs or pngs And then we're saying image detected else. We're saying it's a document The rest of this you guys already know because we've used it in the previous tutorial And if you haven't already you should go ahead and watch that So instead of just saying image detected, let's actually attach the file. This is pretty easy. So what you need to do is type in with open uh The file name which is the variable we've got right here and it points towards the first item that's been uh selected by the user and all the other ones I ignored Um comma and then we're opening in read bytes mode because we need to read the bytes and then we're saying save it as f so it's easier to refer to Now we're going to do in here. We're going to need a new variable called file data Which is going to be all the data that's read from the file. I'm going to do f dot read and then Uh, finally, we're also going to need an image type Which is going to Which is going to need to be Um a module import is required here as well. I forgot to tell you guys before So we're going to have to import a quick module here before we carry on which is going to be import image hdr So what this uh module does is it finds out Basically what format the image is and then actually says the actual format instead of saying jpg It will say jpeg or like the full form of the format. We're going to need that. So we're going to do image type equals image hdr Dot what which is like asking you what type of format are we using and we're passing our file name File name And then we're going to have to do is go back one and then so that we're not in the loop for the width anymore And then do message if you guys remember message was our object that we used before And we use this because we can attach stuff to it now just like the attached a subject and all that We're going to attach the actual document or the image to it right now So message dot add attachment This is one of the methods that's available in the object. So File data, which is the file data. We just created or read from the file now main type equals Image because it's an image type And we also need to specify the sub type Which is why We went ahead with the image hdr. I am g hdr Module to find out the actual image type sub type equals image type and then lastly file name equals F dot name, which will give you like the file name including the directory These are all the arguments you need to attach the file And then once you're done with that your image should send off successfully Now let's quickly test this out to make sure that the image attachments are working so far Just run this Okay quickly log in Already aware this tutorial is a bit long for you guys But I'm trying to really explain pretty much everything so that you guys can actually understand what's going on I really Like when people understand what they're doing is sort of just copying and pasting So yeah, that's what we're going for here. Let's say image Test email now we're going to add the attachment and hopefully fingers crossed we're not going to have any errors So I'm going to go for the jpeg because we've only coded the bit where we can send an image file Now I'm going to click on send And we have an error sending the email So Another type of password right so again Okay, so I'm going to have to see what the error is which is why I'm going to have to Accept let's go ahead and see what this is So I'm going to do Oh Attachment is going to be Set Okay, no module named image hdr What went off there? Let me quickly take a look at What this module was called again I'm in python So I'm going to quickly take a look at how I imported it in one of the previous files I've created So It is Should be this image hdr All right, take a look at what we've done here It's Image doesn't let us paste because of the file I guess so I'm going to quickly check again I am g hdr copy Ah, I see That's so stupid. So I've typed in image hdr instead of i am g hdr here So that's like the most stupid mistake anyone could have done. I've been saying it all along as well Sorry about that Okay, attach a file Let's go ahead and attach the website jpeg and send All right, there was just another error waiting for us. So import image hdr Oh, I've spelled it wrong twice Wow So it's actually i am g hdr my bad So let's try again and so just going to be test Let's try Again Attachments Desktop website that jpeg sent Taking a bit of time perfect. It's sent perfectly. So now I'm going to go ahead and check my email Um, bear in mind if you're sending it and checking it straight away It might take a quick second to load because it's an image file. So I'm going to go ahead in my email And guess what it says Let's try again and we have our little bear picture that I've sent through The how cool is that guys? So you've been able to send your image files Now it's going to be pretty basic and straightforward to actually Send your documents. So what you need to do is copy this section right here So from the width open until your message dot attach We're going to copy that and then we're going to replace this paste thingy With that now you have to make a few changes here We don't need the image type anymore Because we don't need to deal with that in this protocol. We don't need that So all you need in the second one is We need to open the file then we need to read the file data and then we need to attach it And while attaching we attach the file data, which is right here Then the main type needs to change to Application because that's just how it calls for That's just how it is in the documentation. So we've got to follow that and the sub type is going to be Um We have to set it to like a stream So that's how it is in the documentation. So we've got to follow that as well octet stream stream cool And now we have the file name which equals f.name and that's it that should have Done our second one as well now Let's run this quickly to take a look if this works and i'm going to try and attach a document up So hopefully it works subject Checklist because it's like an excel document. I'm trying to send Please from the file attachment Let's go to my desktop and decision plans. Perfect. Sint Oh, that is nice to see no errors whatsoever. I mean we've only copied and pasted but hey, it's nice to see no errors Let's close this and go forward and check if I have received this email Hey, it says checklist and then it says please find the attached file And then I actually have my spreadsheet file showing up right here, but click on it. It actually shows up So guys that was it for today's tutorial. Hope you've enjoyed it I'm really trying my best to kind of create tutorials that aren't really out there already and can help you like Add these unique projects to your portfolio and learn more So I hope you've enjoyed that. Please make sure to like comment subscribe share Sharing is kind of the main thing right now because i'm just coming back to youtube So kind of try and share my videos as much as you can if possible And if you can leave me any feedback, that would be great as well Any ideas you guys have for the next video, please make sure to leave them in the community tab under the post that i've done And i'll see you in the next one guys peace out