How to exec a php script when receives an email

Discussion in 'Tips/Tricks/Mods' started by elcore2010, Jan 20, 2010.

  1. elcore2010

    elcore2010 New Member


    How can i configure an account to parse each ermail received with a php script?

    Thank you!
  2. edge

    edge Active Member Moderator

    I'm doing this on one of my sites email's (on ISPconfig 2)

    You will need to add the following in /etc/aliases
    web96_info: "|/var/www/web96/web/script.php"
    After this run newaliases to re-create the aliases db.

    Now all email going to web96_info will be "piped" to /var/www/web96/web/script.php

    More info here:
  3. elcore2010

    elcore2010 New Member

    thank you, but web96_info is the user, what happens if we had two emails web96_info into 2 domains?
  4. edge

    edge Active Member Moderator

    You will need to set web96_info to catch all emails, and let the "script.php" handel the rest
  5. elcore2010

    elcore2010 New Member

    so, if i had 2 email addresses in the server such: [email protected] and [email protected] one alias such as support will handle theses 2 adresses??

    could we get only ONE of thies email??
  6. edge

    edge Active Member Moderator

    Sorry.. My misstake.
    It will only work for one domain.
    So if you need it for two you will need to set it up for both domains.

    web96_support: "|/var/www/web96/web/script.php"
    web97_support: "|/var/www/web96/web/script.php"

    web96_support is for [email protected] and web97_support is for domain [email protected] (you will need to change the web96 and web97 to your own number)

    You can point both to the same script.php
  7. elcore2010

    elcore2010 New Member

    OK, I understand, so webX is the internal ID of ISPCONFIG fort each domain...

    I'm doing it but i have errors:

    email to parse: [email protected]
    the email accoutn is not created in ispconfig

    in /etc/aliases I have added:
    web4_support: "|/var/www/clients/client0/web1/web/cli/index.php"

    web4 because i see in /var/www/clients/client1: -> /var/www/clients/client1/web4/

    but when i send an email to [email protected] it returns it and i see in /var/log/mailog: Recipient address rejected: User unknown in virtual mailbox table

    PD: is not my domain :)
  8. edge

    edge Active Member Moderator

    did you run the command newaliases, and did you setup an account for to CatchAll ?
  9. elcore2010

    elcore2010 New Member

    yes, i have run newaliases to recreate them.

    i have not catchall, iu have created now, but it doesn't work, the email is redirected to other email address but the pipe doesn't work...
  10. edge

    edge Active Member Moderator

    It could be a permission problem!

    I have this running on a ISPconfig 2 server (on: [​IMG]).
    Tomorrow I will test all this on my ISPconfig3 server.

    I'll report back as soon as I know more.
  11. edge

    edge Active Member Moderator

  12. edge

    edge Active Member Moderator

    Found it I think.

    Try: "|/var/www/clients/client0/web1/web/cli/index.php"

    and than newaliases

    Let me know if it worked for you.
  13. edge

    edge Active Member Moderator

    I've done some more testing on my ISPconfig 3 server, and I can for some reason not get it to work.

    Let me know if you find a way to make it work.

    Again.. Its working great on a ISP2config setup server.
  14. BorderAmigos

    BorderAmigos New Member

    How I got this to work...

    Since ISPConfig3 wants me to have a domain name with the email addresses I added a mail forwarding entry of "[email protected]" forwarded to script.

    In "/etc/aliases" I added "script: |/var/www/some_directory/email_script.php". Then run "newaliases". Note that the directory is not in a web root of any client.

    "email_script.php" contains...
    // read from stdin
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd)) {
    	$email .= fread($fd, 1024);
    // handle email
    $lines = explode("\n", $email);
    // empty vars
    $from = "";
    $subject = "";
    $headers = "";
    $message = "";
    $splittingheaders = true;
    for ($i=0; $i < count($lines); $i++) {
    	if ($splittingheaders) {
    		// this is a header
    		$headers .= $lines[$i]."\n";
    		// look out for special headers
    		if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
    			$subject = $matches[1];
    		if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
    			$from = $matches[1];
    	} else {
    		// not a header, but message
    		$message .= $lines[$i]."\n";
    	if (trim($lines[$i])=="") {
    		// empty line, header section has ended
    		$splittingheaders = false;
    preg_match("/boundary=\".*?\"/i", $headers, $boundary);
    $boundaryfulltext = $boundary[0];
    if ($boundaryfulltext!="") {
    	$find = array("/boundary=\"/i", "/\"/i");
    	$boundarytext = preg_replace($find, "", $boundaryfulltext);
    	$splitmessage = explode("--" . $boundarytext, $message);
    	$fullmessage = ltrim($splitmessage[1]);
    	preg_match('/\n\n(.*)/is', $fullmessage, $splitmore);
    	if (substr(ltrim($splitmore[0]), 0, 2)=="--") {
    		$actualmessage = $splitmore[0];
    	} else {
    		$actualmessage = ltrim($splitmore[0]);
    } else {
    	$actualmessage = ltrim($message);
    $clean = array("/\n--.*/is", "/=3D\n.*/s");
    $message = trim(preg_replace($clean, "", $actualmessage)); 
    // Everything after here is just for testing...
    $from="From: ".$from."\n\n\n";
    $subject="Subject: ".$subject."\n\n\n";
    $message="Message: ".$message."\n\n\n";
    $headers="Headers: ".$headers."\n\n\n";
    mail('[email protected]','Script Response', $email);
    // Directory must be 777 to write to it...
    $myFile = "/var/www/some_directory/email_script_test.txt";
    $fh = fopen($myFile, 'w');
    fwrite($fh, $email);
    Last edited: Feb 17, 2010
  15. edge

    edge Active Member Moderator

Share This Page