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 [email protected]. 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.

1
  • 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, ...) Commented Jun 11, 2013 at 13:34

2 Answers 2

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.