run a script based on incoming email

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cgcullen
    Member
    • Mar 2004
    • 34

    #1

    run a script based on incoming email

    I'm trying to kick off a php script when an email is sent to one of my email addresses. I read in an earlier script that I can set up an email alias and pipe it to a php script.

    I need to know what path I should use in my forward to my php script. If I put my php script in the public_html folder, should I forward an email to this path?

    testing@mydomain.com forward to |/home/user/public_html/script.php


    Can anyone help please?

    Thanks!

    Cindy
  • sdjl
    Senior Member
    • Mar 2004
    • 502

    #2
    I think the syntax is:
    Code:
    php /home/user/public_html/script.php
    You may want to double check that though.

    I have in the past had issues when adding a pipe to a forwarder address as cPanel removes spaces when adding it. You may have to get support to add the correct pipe for you.

    I would maybe look at setting up a POP3 box and then using a cronjob every 30minutes or so to use sockets or the other mail functions to check the inbox and do something dependant on an email being there or not.

    David
    -----
    Do you fear the obsolescence of the metanarrative apparatus of legitimation?

    Comment

    • Amitabh
      Member
      • Mar 2004
      • 78

      #3
      I am not sure, but sockets are not allowed on Dathorn servers...?

      Comment

      • Dan
        Member
        • Mar 2004
        • 99

        #4
        I have a client using some software that required a pipe to be setup in a forwarder to run a cgi script. It required that the pipe be enclosed in quotes:

        "|/home/user/public_html/cgi-bin/file.cgi"

        I do not know if php can be run the same way though...

        Hope that helps

        Dan

        Comment

        • cgcullen
          Member
          • Mar 2004
          • 34

          #5
          update

          Ok, it's sorta working...

          I have it in my cpanel forwarder as:

          testing@mydomain.com -> |/home/user/public_html/test.php

          (no quotes)

          It's kicking off my php file which just does a mail(); to my email address to let me know it worked.

          But, I'm also getting a bounce message as well which says:

          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 |/home/user/public_html/test.php
          generated by testing@mydomain.com

          The following text was generated during the delivery attempt:

          ------ pipe to |/home/user/public_html/test.php
          generated by testing@mydomain.com ------
          Any ideas on how to get rid of the bounced message?

          UPDATE again:

          If I use quotes around the pipe, I don't get any email - but it doesn't bounce either. It just disappears! Any ideas?

          Thanks,
          Cindy
          Last edited by cgcullen; 01-18-2005, 12:38 PM. Reason: To update the update :)

          Comment

          • sdjl
            Senior Member
            • Mar 2004
            • 502

            #6
            Could try:
            Code:
            testing@mydomain.com -> |/home/user/public_html/test.php > /dev/null
            David
            -----
            Do you fear the obsolescence of the metanarrative apparatus of legitimation?

            Comment

            • cgcullen
              Member
              • Mar 2004
              • 34

              #7
              I'm still getting a bounced email.

              Any other ideas?

              Cindy

              Comment

              • Buddha
                Senior Member
                • Mar 2004
                • 825

                #8
                You are accepting the input from the pipe?

                Code:
                    $stdin = fopen("php://stdin", "r");
                    while (!feof($stdin)) {
                        $buffer = fgets($stdin, 4096);
                        $message[] .= $buffer;
                    }
                .... right?
                "Whatcha mean I shouldn't be rude to my clients?! If you want polite then there will be a substantial fee increase." - Buddha

                Comment

                • cgcullen
                  Member
                  • Mar 2004
                  • 34

                  #9
                  This is what I have:

                  #!/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;
                  }
                  }

                  mail('me@mydomain.com', $subject, $message, "From: admin@mydomain.com");

                  ?>

                  Instead of using the cpanel forward, I tried the cpanel email filtering instead. I don't get the bounced email, but the script doesn't seem to get executed either. It just disappears.

                  I'll keep playing around with it...

                  Thanks,
                  Cindy

                  Comment

                  • Buddha
                    Senior Member
                    • Mar 2004
                    • 825

                    #10
                    Exim could be configured to return any standard output as a delivery error. Could also be config'ed to send a delivery error on failure too.

                    My first instinct is stray whitespace, got any outside the php tags?

                    Beyond that just guessing:

                    1. It's the shebang on the first line getting outputted. Does it work without it?

                    2. The shebang needs a flag: #!/usr/bin/php -f
                    Could use -q too.

                    3. Any php errors in your error log? Doubt this didn't get any myself.

                    Really reaching here ... best I come up with.
                    "Whatcha mean I shouldn't be rude to my clients?! If you want polite then there will be a substantial fee increase." - Buddha

                    Comment

                    • cgcullen
                      Member
                      • Mar 2004
                      • 34

                      #11
                      Thank YOU!!

                      It worked! I tried taking the shebang out - still the same thing.

                      But, I added the -q and it seems to be working great!

                      Thank you VERY much!!

                      Cindy

                      Comment

                      • sdjl
                        Senior Member
                        • Mar 2004
                        • 502

                        #12
                        So just for anyone who happens to stumble across this post in the future, what setup do you now have?
                        How is the forwarder setup and what's the syntax of your script to get things working?

                        Thank you!

                        David
                        -----
                        Do you fear the obsolescence of the metanarrative apparatus of legitimation?

                        Comment

                        • Buddha
                          Senior Member
                          • Mar 2004
                          • 825

                          #13
                          Originally posted by cgcullen
                          It worked! I tried taking the shebang out - still the same thing.

                          But, I added the -q and it seems to be working great!

                          Thank you VERY much!!

                          Cindy


                          I agree with sdjl a summary would be nice.
                          "Whatcha mean I shouldn't be rude to my clients?! If you want polite then there will be a substantial fee increase." - Buddha

                          Comment

                          • sdjl
                            Senior Member
                            • Mar 2004
                            • 502

                            #14
                            I don't think we're going to get a review
                            -----
                            Do you fear the obsolescence of the metanarrative apparatus of legitimation?

                            Comment

                            • cgcullen
                              Member
                              • Mar 2004
                              • 34

                              #15
                              Summary

                              Sorry guys - been away from my desk. Here's what I did:

                              In Cpanel, I went to the forwarders (it also works to go in E-mail filtering) and entered this forwarder (or filter):

                              myemail@mydomain.com -> |/home/user/public_html/test.php

                              I tried it with an email account set up for myemail@mydomain.com and without and so far, it's working great either way.

                              test.php looks like this:

                              Code:
                              #!/usr/bin/php -f
                              <?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;
                                  }
                              }
                              mail('me@mydomain.com', $subject, $message, "From: admin@mydomain.com");
                              return NULL;
                              ?>

                              I got most of this script from this article:
                              Founded in 1998, evolt.org was one of the oldest Web development communities in existence. Through our mailing lists, articles, and archives, we have strived to work together in sharing our collective knowledge to improve the Web for all of us.



                              I also had to make sure that all stray spaces & hidden characters were out of the code that I downloaded from the article.

                              I hope that helps!

                              Cindy

                              Comment

                              Working...