2

I have a web site hosted with a cPanel web host.

I am looking to have a PHP script to run every time I get a new email to help@xxx.com. The script that I was hoping run will update some vars on a sql data base.

I know how to accomplish all the tasks with PHP, the only part I am trying to figure out is how to tell my web server to run the script when a new email is received.

currently running a cron job and would prefer to not run the script every min and only when relevant.

PHP
  • 1,699
  • 5
  • 24
  • 43
  • 1
    You can configure your mail service to run a script every time a mail arrives. How exactly depends on the program you have running (e.g. postfix, procmail, ...) – Gerald Schneider Jun 11 '13 at 13:34

2 Answers2

2

On most cPanel hosts, you can set a forwarder to forward an email to a script when it is received. As long as your email is managed on the same cPanel account, it will work great ...

When you create the forwarder, you will set the forward to address as |/home/username/path/to/script.php

Then the first thing you need to do in the script is receive the email from stdin:

//Receive the email from stdin ...
$fd = fopen("php://stdin","r");

$data = '';
while(!feof($fd)){
    $data .= fread($fd,1024);
}

fclose($fd);

//process email data ...

I've done this on a number of cPanel hosts with great success.

keithhatfield
  • 3,273
  • 1
  • 17
  • 24
1

If you have access to your MTA (Mail Transfer Agent) configuration, you could set up a script that is triggered when new mail arrives. How this is done depends on your MTA.

Also, you could use procmail or a .forward file and set either of them up to execute a script when email is received to the desired address.

Community
  • 1
  • 1
Kaivosukeltaja
  • 15,541
  • 4
  • 40
  • 70