Processing incoming mail with PHP

Discussion in 'Game Development (Technical)' started by Valen, Aug 7, 2004.

  1. Valen

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    133
    Likes Received:
    0
    This may not be something that's done very often (unless you're writing an online mail system I guess) but I'm trying to figure out how to pipe an incoming email to a PHP script. I'm hosting with pair.com (they run freeBSD), and their control panel allows you to do this. The problem is, it doesn't work for me. :)

    What I tried to do is pipe mail to a script which then uses php://stdin to try to grab the email. I found one article (seems to be plastered all over the web) that shows how to do this -- http://gvtulder.f2o.org/articles/incoming-mail/ From my observation, it seems that the script is never executed (either that or it can't grab the email). It's very strange because if I send an email, it doesn't bounce. But once I delete the script file from the server it instantly bounces.

    Any *nix experts here who can help? :)

    Here's the code (pretty much like in the article except I try to dump it to a file)

    Code:
    <?php
    
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd)) {
        $email .= fread($fd, 1024);
    }
    fclose($fd);
    
    $outfile = fopen("test.txt", "w");
    fwrite($outfile, $email);
    fclose($outfile);
    
    ?>
     
  2. oNyx

    Original Member

    Joined:
    Jul 26, 2004
    Messages:
    1,212
    Likes Received:
    0
  3. Sillysoft

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    831
    Likes Received:
    0
    I have never used PHP to read from stdin, however in other languages you can read from stdin forever without getting an EOF (unless the user types control-d or something). So my guess is that you are forever blocking in the 'while (!feof($fd))' loop. It is just a guess, you could test it by writing to a file within the loop (or something).
     
  4. Valen

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    133
    Likes Received:
    0
    Neither of those seem to be a problem. I added code to send an email to me before the main code runs, and after. I get the emails when I run the script directly from a browser, but sending email to the script doesn't do anything. Looks like it's not being executed. What I don't get though is why. :confused: I'm not getting bounced emails, so it knows that the script is there but it's not executing. Maybe I'm missing some kind of setting somewhere.
     
  5. Valen

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    133
    Likes Received:
    0
    [edit] After combing some PHP mailing lists, I figured out how to get it to work. In case anyone searches for this, my solution was to give qmail the script path like this:

    /var/qmail/bin/preline /usr/local/bin/php /script_path/script.php
     
    #5 Valen, Aug 8, 2004
    Last edited: Aug 9, 2004
  6. Kai Backman

    Original Member Indie Author IGF Finalist

    Joined:
    Jul 28, 2004
    Messages:
    297
    Likes Received:
    0
    Just to give a second solution. I simply have a script that uses Curl to post the email to a specific PHP page. For testing the page always returns a form where you can enter the email into a textbox and run the script again. As the previous mail is stored in the textbox this gives a faster turnaround (and enables manual processing of emails).
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer