The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

forward All recieved mail to a PHP file

Discussion in 'E-mail Discussions' started by nasser110, Sep 13, 2007.

  1. nasser110

    nasser110 Member

    Joined:
    Sep 13, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Hello
    Please Help me , i want forward All recieved mail to a PHP file , because i want create a virtual email system where any user from my site create a Valid email adress example : any@mysite.com for this work i want redirect all recived mail to a php file where that php file inserted into myDB
    tnx
     
  2. nickp666

    nickp666 Well-Known Member

    Joined:
    Jan 28, 2005
    Messages:
    770
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    /dev/null
    search the forums for information about piping e-mail to a script, once you have the syntax you need set the default address on the domain to the pipe
     
  3. nasser110

    nasser110 Member

    Joined:
    Sep 13, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    Hello , very thank you , but a problem , when any mail recived for me , and redirect to php script All text are saved to php file and my php file does'nt execute , i am use the #!/usr/bin/php in first line in my php code but not solved problem , and entire text in recived mails saved into php file
    please help me
    tnx
     
  4. nasser110

    nasser110 Member

    Joined:
    Sep 13, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    No body cant't help to me ? please tell me , my php script does'nt execute in piping mail , and entire text from piping mail are saved to my php script !!
     
  5. sparek-3

    sparek-3 Well-Known Member

    Joined:
    Aug 10, 2002
    Messages:
    1,381
    Likes Received:
    23
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    If you are using the shebang line then you need to make sure that the PHP script is executable.

    You also need to make sure that the PHP script is not outputting anything, even a blank line can cause problems.

    Just do a quick run through of the script and see if everything is in order. You need to access the script from a shell, ideally as whatever user the script is going to be running as. So you really need to SSH into the server as that user, you may need to enable shell for that user to do the test.

    Then run the script:

    /path/to/phpscript.php >output.txt

    The shell will just sit there there, its waiting for input (if the script is coded right -- if you get an error or if the script exits, then you have a problem).

    Copy and paste a complete e-mail message, depending on how you've got the script written, headers may be necessary.

    When you have pasted that into the shell as the script is waiting for input, hit Ctrl+D to close out the input. The script should process the input and perform whatever task is needed.

    If you get an error, then you have a problem.

    Review the output.txt file. Is it empty? Does it contain anything? If it has any carriage returns or anything like that, then you have a problem.

    Depending on if and where you have a problem determines your next course of action. Unfortunately, I doubt very much that anyone here would be able to help that much as any problems would be a result of a coding error somewhere in the script.
     
  6. nasser110

    nasser110 Member

    Joined:
    Sep 13, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    1
    hi , very thank you , my script is :
    Code:
    #!/usr/bin/php
    <?php
    // read from stdin
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd)) {    
    	$email .= fread($fd, 1024);
    }
    fclose($fd); 
    // 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; 
    	       	               }
    } 
    	       	                //For Testing Purposes
    	       	                $outFile = fopen("emails.txt","a");
    	       	                fwrite($outFile,$from);
    	       	                fclose($outFile);
    	       	                mail("myemail@yahoo.com", $subject, $message); 
    	       	                ?> 
    
    maybe problem in this my code? because now i have not access to SSH . maybe this probelm depend on modular or CGI install PHP ?
    meanwhile my folder and this php script are CHMOD 777
    tnx
     
    #6 nasser110, Sep 15, 2007
    Last edited: Sep 15, 2007
Loading...

Share This Page