if you are writing a mailserver that accepts incoming mails directly by SMTP, then that script will run from PHP-CLI (command line interface) only and not as a script.
whereas you are just looking foreard to reading the mail recieved the server running in linux, then you will need to read the mail folder in linux and analyse file, contents there to achieve the task
you mught like to have look at source codes of squirrelmail (squirrelmail.org) and neomail (neomail.org)