Redirect all incoming mail into a script

Solution 1:

When you say "a PHP script" do you mean a PHP script on a webserver elsewhere, or a PHP script run on the command line locally?

I've done sending the mail to a website elsewhere using exim4 and curl, by creating a custom transport like so:

send_to_site:
  driver = pipe
  command = /usr/bin/curl https://example.com/mail.php --data-urlencode "mail@-"
  user = nobody
  group = nogroup
  return_path_add
  delivery_date_add
  envelope_to_add

If you are using Debian's "split configuration" option, you would create a file in /etc/exim4/conf.d/transport/ with this in it. The command here will pass the entire email (headers and body) to mail.php in the variable $_REQUEST["mail"]. You'll need to have your PHP script handle the headers.

To trigger the transport you'll need to have a router configured that matches whatever email you want to receive and uses the above transport to send it. With split configuration, routers go in /etc/exim4/conf.d/router/. For capturing ALL the mail for a specific domain, I haven't tested this but I think this is right:

catchall_mail:
  driver = accept
  domains = mydomain.com
  transport = send_to_site

Debian numbers the files in the router directory to set the order routers are checked in. The first matching router will be used to handle the email. From my configuration here, you'd probably want to number yours around 450 to go after aliases and before the routers that handle local users like hubusers and procmail.

After adding these files to the transport and router directories, you will need to run update-exim4.conf to have Debian create the configuration file exim actually reads.

Solution 2:

You need to set up either a POP3 or (preferably) an IMAP server. Then create your email boxes there as normal. Use the appropriate PHP functions to simulate a mail client and periodically poll the email boxes for new messages.