 and welcome. Today, we're going to be looking at extracting attachments and messages from an email. So, when you send someone an email, it goes to their email server. It's actually stored on that server, well, depending on the server setup, but in many cases, you have what's called an inbox file or a mailbox file, which is the plain text file. And emails are completely plain text. So, I have an example here. I have an email file that has an inbox file that has three files in it against plain text. The name and extension doesn't really matter for our uses here. But, if I list out here, you can see that I have a file called toot, which is short for tutorial since this inbox file specifically for this tutorial. I can go into this file and you can see what it looks like. It has who it's from, who it's to, the names, the servers, the timestamps, blah, blah, blah. If I move down and I search for attachments, you can see that we have here, it says it's an attachment. Let's go down to the next one for a better example. So, it's plain text, but it's format, a specific way. Each email has its own little, little unique tag here. In attachments, it will tell you what type of attachment it is. It's an image. It's a JPEG. Here's the file name. It's an attachment. And it's encoded in base 64, which is at least how I know. I've always seen it like this. So, if I've talked about this in the past, base 64. Base 64 takes any file, whether it be binary or not, and it converts it to a plain text file. So, if you have a picture, a music file, a PDF, anything that's a binary file that isn't plain text, it will convert it. And that's what this, all this is right here, all these letters, it, that is our image. This is our JPEG, but convert it to plain text. And that is how attachments are sent through email, through their plain text. And as you can see, an image takes up a lot of text. And this makes it very easy to transfer data different ways because, you know, all computers accept plain text. But how do we take that out and decode it? Now, you could write your own script, which I've done this in the past, where I'll look for the base 64, extract that out, run it through base 64 to put it in a file. Then I have to look and find the file name, so make sure I put it into the file, so it's not just randomly named and it has the right extension, if it's a JPEG or a PDF or whatnot. And that's how an email file is set up. But how do you, without having to write all that scripting yourself, extract attachments? This is very useful. For example, I have filters that filter out different emails to different mailbox files. And then I have scripts that automate that when an email comes in, it does something. For example, the United States Postal Service has a service where they will email you every morning. They will scan all the outgoing mail, the letters, and they will email it to you with an attachment with a picture, with a scan of your snail mail letters. So I know what's going to be in my mailbox before it gets here. And that way I can know, oh, it's an important check coming, a bill coming. I know. And then if I go out to the mailbox when the mail comes in, it's not there. I can report it as stolen. It's also just nice to know what's coming later today. And as a side note, criminals have been using this because whether you're using that service or not, the United States Postal Service offers it to you and it's very easy to sign up. But if you don't sign up, it allows someone else to, with just a little bit of information about you, sign up for you. And then they'll start receiving your emails and they can then know ahead of time whether you're going to be getting a check or something important or they can have stuff sent to your house and they know when it's coming and then go out to the mailbox and get it before you do. So it's advised, even if you're not going to use this service, that you should probably sign up for it to help prevent criminals from using it. Hopefully they come up with a better system to prevent that in the future. Anyway, that's one option there. Another thing that I use it for is where I work. When we get paid, they send us our paychecks through email as a PDF, just the stubs because I get direct deposit. And I have all my paystubs filtered out to a certain mailbox file. So all my paystubs are in one mailbox file. So it makes it very easy for me to pull that mailbox file instead of having to go in and go, open this email, download attachment, save to here, go to the next email. I can write a script that automatically can pull those out every when they come in or if I have a mailbox file, pull them all out at the end. So that's what we're going to be looking at today. There's actually two programs I'm going to show you today. I'm going to show you one and then in the next video, I'm going to show you another option that has some features that actually are a little more advanced for this. But I have, again, this one mailbox file. So what I can do is I can use a package. So if I do apt, actually I look for aptitude when I'm searching, use your package manager, you can search for a program called mpack. And it is right here. And if we do aptitude show mpack, as you can see with the I here, I already have this installed. But once you install it, it says right here mpack and I don't know if that's mumpack or I'm going to say mumpack sounds better. Our utilities for encoding and decoding respectively, binary files from mine mine, people may follow the way I was saying this in the last video, which means multi-purpose internet mail extension, format mail messages. So basically it's for encoding and decoding them. So what you can do here, now that it's installed, I can say mumpack and I can point to that to file. And if I didn't already mention too short because it's the mailbox file for this tutorial, tutorial to not like a fart. Anyway, and it went in and it found a an attachment called text, my file dot text. And if I cat that out, you can see that it's the text file, which is great. But we already looked at the fact that there's more than one attachment in this, there's more than one email. And that's the thing that has the drawback for mumpack is that it's going to find just the the one, the first one in the email or is it the last one? No, I think it's the first one. So here's the thing when you set up a mail server, you have options to have all your emails going to one inbox file or have a directory where each email is in an individual file. So you have a folder and for a certain mail address and every email that comes in goes into its own mailbox file. It will still look like this, but for each message and we're broken down. And unfortunately, mumpack doesn't support that multi message multi attachment thing, it's just going to find that first one and attack and extract it. So you can manually split up the files if you want with a script that wouldn't be too hard. But I'm going to show you another program in the next video that will just extract everything will strike the messages, the text messages. And actually, if I give mumpack another option, so another option, if I run that again, mumpack on that file, you can see it says this file already exists and it gives it on the same file name with dot one. If I run it again, it's going to give it dot two. If I give it dash f option, it's going to tell you that that file already exists, but it overwrites it. It doesn't create a new file with an extension. Another option, if you were to look into the man page for mumpack, you can see that there's this dash t option, which writes text file parts of multi part messages as a file. So you might think, oh, so we have multiple emails in here. That's the option for splitting this up. And no, what that's going to do is it's going to, if I show you here, let's go ahead and clear the screen. We'll do dash f for force override and t for that split option and give it that. And it's going to create three files. And what it's creating here is it's taking that first email and splitting it up into its three sections, but it's still ignoring this last two emails or any other email in there. And the different parts are when you send an email, if it's formatted, and a lot of email clients do this and a lot of people frown upon it, is if you have to do formatting, bigger text, color text, it's actually sending it as HTML. And a lot of clients, even if you don't put that in there, it's still defaults to HTML. And a lot of people don't like that because it increases the size of the file for a number of reasons. And also if you have a client that doesn't support HTML, such as I use mutt, which is a text-based client. And by default, it's if when I get an email, if it's an HTML format, I'm going to see all the HTML tags, it makes it hard to read, but I actually run it through a script that strips out the HTML and formats it somewhat right for me. But when it sends it as HTML, it also sends a copy in plain text. So when I get it in, I'll have a plain text copy and the HTML copy. So now you've doubled up on that, but then you also add the tags, which also increases the size of the file. But that's what it's doing. It's split up the plain text, the HTML, and that attachment. And if I list that, you can see they're all here. So I can cat out, again, you know, parts of this message, part two will be the HTML. But again, it's still only seeing that first email. In the next video, we'll look at another program that will rip out everything from this single mailbox file. So again, you have options, M unpack, we're great if all your emails are going to separate inbox files. But if you're having them all go into one inbox file, which is fairly common, you're either going to have to write a script that splits it up for you and then runs it through M box, or I'm sorry, M unpack, or use the next script. So watch my next video. If you're interested in doing that, I think if you're watching, please visit filmsbychris.com. That's Chris of the K. There is a link in the description to my page and also link in the description to patreon.com forward slash metal x 1000, which is a place you can go and support me on a monthly basis if you can. If you don't like Patreon, because a lot of people are having issues with Patreon, having looked into why political reasons, I would assume you can also support me through PayPal. There's a link if you go to filmsbychris.com again, Chris of the K link in the description support section. There's a link there to support me through PayPal. And as always, I appreciate your support, watching, liking, sharing, subscribing, commenting, all that stuff. Have a great day.