AlphaOne Technology Support Forums
Welcome, Guest. Please login or register.
January 09, 2009, 04:21:47 PM

Login with username, password and session length
Search:     Advanced search
1733 Posts in 827 Topics by 5370 Members
Latest Member: aceddinue
* Home Help Search Login Register
AlphaOne Technology Support Forums  |  Server Related Discussions  |  E-Mail Issues  |  Topic: Forwarding incoming emails to a PHP script 0 Members and 0 Guests are viewing this topic. « previous next »
Pages: [1] Go Down Print
Author Topic: Forwarding incoming emails to a PHP script  (Read 1368 times)
Scott Chase
Sr. Member
****
Offline Offline

Posts: 20


go fish


View Profile WWW
Forwarding incoming emails to a PHP script
« on: June 13, 2006, 04:41:49 PM »

I have been researching how to forward emails to a PHP script.  I receive emails from a form on another company's website.  The only method to receive that data currently is via email.  So I want to parse the emails and store the data in MySQL.  I've seen a few articles around that talk about a .forward file in my root directory.  But I don't want every email address to be forwarded -- just a single email address.  They also mentioned that you could do it with an alias file found in the /etc directory.

I did not find an alias file but I wondered if I created an alias from CPanel if I could put the path to the PHP script or does it have to be an email address?

Does anyone have any experience with something like this that you could share?

Logged

Providing managed hosting at
http://www.chaseserver.com
AlphaWolf
AOT Administrator
Administrator
Hero Member
*****
Offline Offline

Posts: I am a geek!!



View Profile WWW
Re: Forwarding incoming emails to a PHP script
« Reply #1 on: June 14, 2006, 05:58:39 AM »

Yes it is called piping and we use that for our tech support application.

Instead of putting an email address in the forward in cpanel you put in a pipe similar to this

|/path/to/pop3pipe.php

Logged

AlphaOne Tech Webmaster Resources
http://www.alphaone-tech.com/resources/
Scott Chase
Sr. Member
****
Offline Offline

Posts: 20


go fish


View Profile WWW
Re: Forwarding incoming emails to a PHP script
« Reply #2 on: June 14, 2006, 09:47:45 AM »

Thanks for the response Wolf.  Actually I was coming back here to post an update.  In the early hours this morning I discovered that about the piping.  And I just now finally got the script working like I want.  I'll post some here because it was such a struggle for me, maybe it will help someone else.

I have my forwarder pointed to > "| /full/path/to/cgi-bin/script.php"
I have chmod the script to 755

The start of my script is:

Code:
#!/usr/bin/php -q
<?php

// read from stdin
$fd fopen("php://stdin""r");
$email "";
while (!
feof($fd)) {
  $email .= fread($fd1024);
}
fclose($fd)

Some code here

The end of my script

Code:
mail($emailto,$subject,$message);
exit();
?>

After discovering the piping, i was able to get the emails to the script and the script would parse the email and pull out the variables exactly as I wanted.  Then i create another email with those variables and email it to me just for testing purposes to make sure the script is parsing the original correctly.  The email did make it to me with all the variables.  All that is great!  The downside was that I kept getting a returned email from the server (mailer-daemon) saying

Quote
This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

 pipe to | /full/path/to/cgi-bin/script.php
   generated by test@_mydomain_.com

The following text was generated during the delivery attempt:

------ pipe to | /full/path/to/cgi-bin/script.php
      generated by test@_mydomain_.com ------

X-Powered-By: PHP/4.4.2
Content-type: text/html

What i discovered this morning was that in the first line of the script you need to have the -q directive.
Quote
#!/usr/bin/php -q

After adding that directive, the script is still receiving the emails and parsing but I am not getting the bounced email now.  Hope this helps someone.  cool

Scott
-----------------------------------------
http://www.chaseserver.com
Logged

Providing managed hosting at
http://www.chaseserver.com
AlphaWolf
AOT Administrator
Administrator
Hero Member
*****
Offline Offline

Posts: I am a geek!!



View Profile WWW
Re: Forwarding incoming emails to a PHP script
« Reply #3 on: June 14, 2006, 11:29:24 AM »

Ah good call - I forgot the -q on the end.

Peace

Wolf
Logged

AlphaOne Tech Webmaster Resources
http://www.alphaone-tech.com/resources/
Pages: [1] Go Up Print 
AlphaOne Technology Support Forums  |  Server Related Discussions  |  E-Mail Issues  |  Topic: Forwarding incoming emails to a PHP script « previous next »
Jump to:  

Powered by MySQL Powered by PHP AlphaOne Technology Support Forums | Powered by SMF 1.0.7.
© 2001-2005, Lewis Media. All Rights Reserved.
Valid XHTML 1.0! Valid CSS!