Using the PHP imap() function

Ok here’s what I want to do… I want to write a script that will automatically catch all emails and auto-respond based on what is in the content of the email… For example I know how to create auto-responders but they send the same email everytime… I want a script that first off checks the email account (imap()) and then responds based on the user email… eventually I would like to turn my server into a text sending server sending texts to cellphones (I can do that already) but I want it so users can send in a question from their phone and the server will answer them back correctly.

Any thoughts on how to go about doing this?