a number of ways.
the easiest is to use procmail or something to filter incoming emails and take an action based on their content. this has nothing to do with php.
the alternative that I use is to set up a cron job to attach to an imap store and download new messages. do some inspection of them and decide whether something is an email reply to a ticket, in which case strip the relevant bits of content and meta data out and shove it in the database.
for a php solution your starting point would be the imap functions in the php manual. plenty of vaguely) adequate examples in that.
for auto - recognition, the easiest is to generate both an x-header in the email header and a backup unique id in the body of the email. store that against the ticket and then search for the shape of the unique id in the parsing code. if you don't get a shape match then reply saying that the email does not correspond with any open ticket and ignore it. if you do get a shape match then carry on with the parsing and storing. so just make sure that the unique id is always in the same format.
the complexity comes with working out how and whether to handle attachments.
this is not hard, but is more complex than it sounds in this post.