Php – Redirect all incoming mail into a script

debianemailPHPpiperedirect

I have no idea about Mail Delivery.

but i need redirect ALL incoming e-mail (*@mydomain.com) into an a php script.

i'm using debian

Exists a simple mail server to do this? (without exim, postfix, etc)

i only need redirect all mail request.

if this is not possible without exim, postfix, etc. how can i do this?

thanks!

Best Answer

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.