There is a class in PEAR that provides facilities for this (though your second sample email looks broken). If you want to do it yourself, you can break the email up into individual lines. MIME sections are separated by lines that start with "--" ("-" doesn't occur in base64-encoded data, so you're in no danger of breaking an encoded file into two pieces). Then take each of those sections and look through the headers (the lines up as far as the first blank line in the section). If there is one that says that the transfer encoding is base 64, then everything from after the blank line down to the end of the section is base64-encoded data. Needless to say, the other headers contain other information about the file, should you want it.