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.

Check for new email msgs script (php)

Discussion in 'E-mail Discussions' started by tcaetano, Sep 8, 2004.

  1. tcaetano

    tcaetano Active Member

    Joined:
    May 15, 2003
    Messages:
    39
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Faro > Portugal
    Hi ppl

    I wanted to do a script but dont know where to start.

    I want to a PHP script that checks frequently (crontab) for new emails on a specific account and sends a new email with the info of the sender and the subject of the email.

    I have a contract with a GSM SMS Gateway, and I send them emails in a special format and then I receive an SMS msg.

    If anyone could help me with this would be great!

    Thanks a lot in advance.. :)

    TIAGO
     
  2. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    do you want this to run from a filter or command line?
     
  3. tcaetano

    tcaetano Active Member

    Joined:
    May 15, 2003
    Messages:
    39
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Faro > Portugal
    Hum... filter or command line? Didnt understood that...

    I want to make a php script and on Cpanel Cronjob feature to ask that to be runned on a regular basis... (ex.: each 5 mins).
     
  4. tcaetano

    tcaetano Active Member

    Joined:
    May 15, 2003
    Messages:
    39
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Faro > Portugal
    Anyone???

    :eek:
     
  5. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    Try this PHP script

    Code:
    #!/usr/local/bin/php
    <?php
    // emailpipe.php - PHP script to read email then parse fields
    
    // you could change "php://stdin" to /home/account/mail/inbox to read inbox
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd)) {
        $email .= fread($fd, 1024);
    }
    fclose($fd);
    // Build the From variable
    $from = strstr($email, "From:");  // Store the From line in a variable
    $strip1 = explode("To:", $from); // Strip out everything before the word To:
    $From =  $strip1[0]; // Store that string in a variable
    //echo $From;   // uncomment for testing
    
    // build the Subject variable
    $subject = strstr($email, "Subject");  // Store the Subject line in a variable
    $strip = explode("Date", $subject); // Strip out everything before the word Date
    $Subject =  $strip[0]; // Store that string in a variable
    //echo $Subject;  // uncomment for testing
    
    // Now the variables contains the From and Subject, we can use them.
    // Build our message for our email
    $msg = "
    $From
    $Subject
    ";
    
    $to = "email@somedomain.com";   // who is sending email
    $subject = "Subject of email";  // what is the subject line of the email
    // Send the email
    mail($to, $Subject, $msg, "From:" .$to);
    ?>
    
    Your cron could run with /path-to-file/scriptname.php < /path-to-inbox
    ex: /home/username/emailpipe.php < /home/username/mail/inbox

    This will read the stdin from the inbox then create a message with the Subject and From.
    The problem is, your inbox will remain so you may need to clean it out. This is why I mentioned using a filter, this way each time the account recieves email the script will process only the email as it arrives.

    See the filters in your cPanel for more information.
     
  6. tcaetano

    tcaetano Active Member

    Joined:
    May 15, 2003
    Messages:
    39
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Faro > Portugal
    Dont want to push on your good help... :) ... but can you explain me a little better how I can use filtering to prevent of keep reading several times the same message and send new notifications from a message already notified?

    Have a few PHP knowledge but not on this "side"...!

    Thanks for all your precious help

    TIAGO
     
  7. asmithjr

    asmithjr Well-Known Member

    Joined:
    Jun 13, 2003
    Messages:
    475
    Likes Received:
    1
    Trophy Points:
    18
    save the script in a directory of your choice. Your Home directory would work.

    chmod 755 the script to mak eit executable.

    Go to cPanel->Mail->E-Mail Filtering
    Add a new filter
    Choose your Filter option (To, contains etc)

    In the Activate field put the PIPE to your script
    Code:
    | /home/accountname/scriptname/php
    
    Test it by sending email to the account with information you wish returned.
     
Loading...

Share This Page