For this solution, you will need to access the mailbox using IMAP. Chances are, this is already supported.
Next, you can grab the file attachment into a $data variable (without needing to save it). See this bountied answer: Retrieving file attachment. Then, you should be able to create your csv using your code (which of course you've put into a function now, right? :)) If you have questions on accessing mail via IMAP, search StackOverflow, try it out using examples from PHP Manual and then post any new questions.