PHP to the Rescue
In the course of moving, many documents must be emailed/faxed/telekinetically transferred back-and-forth between various parties. One of these documents came as an email attachment that wouldn’t open. We requested that the sender resend it, but I decided to go ahead and see if I could rescue the attachment (it was supposed to be a PDF) anyway. For some reason, the attachment had a .msg extension, so I tried to open it in Outlook. Didn’t work. I was more successful using Thunderbird, the Mozilla Foundation’s email client that was made as a companion to Firefox. Thunderbird couldn’t get it right either, though, but it at least opened it in a plain text format so that I could see the source code of the .msg file (equivalent to opening it in Notepad).
Because the email message code shows all of the headers that are prepended to every email message, I could tell that the attachment part of the message was encoded using the base64 algorithm, which isn’t really secure, though it is good enough to keep the encoded file or message from being readable by the average human (unless you’re really smart). Base64 is also the encoding used for HTTP passwords on websites such as private company networks and hidden control panels such as the cPanel administration area that most web hosts use.
Since the base64 algorithm is so widely used on the Internet, I decided to check and see if PHP had a function that could decode a base64-encoded string (a chunk of text). Lo and behold, it did. In seven lines of PHP, I was able to make a script that read the messed up message, pulled out the base64-encoded PHP attachment, decoded it, and rewrote the resulting code to a new PDF file. Mission accomplished.
As it turned out, our new address was wrong on the document, so the sender had to send a new one anyway. But this was the first time I ever applied PHP to a real-life problem like that, so it was kind of cool.