Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: email parsing

  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    email parsing

    Well I didn't know where else to put this so i put it here. I was going to put it in PHP section because I code in that but this function might not be possible in PHP.

    I currently created a website just for me to track gas fill ups (price, gallons, day, miles, total miles) so i can cry every time i fill up and look at my charts :P

    Currently I just write down the numbers every time i fill up and then input it on the website later. What I'd like to do is send a text to a email that I setup on the server and it checks it periodically and parses the email then adds it to the database
    my text would look something like
    m244.1 tm93453 gp3.91 g15.761

    miles, total miles , gas price, gallons

    My question is it possible to create a script with php or really anything web language that checks and email and parses the email then deletes the email. so i can send a text to that email and it will add it. I've tried searching on the internet and they have programs that you buy that do that but id rather not do that.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Im not a php guy but your question makes me think .

    I found this, perhaps it might help?

    http://ericlondon.com/configuring-se...ail-php-script
    http://psoug.org/snippet.htm/Automat..._email_157.htm

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts
    excellent *rubs hands together with maniacal laugh*
    the first link looks promising, I didnt even think of forwarding it to a script even though i dont really know how to do that. I will try that sometime, maybe this weekend
    Thanks

  • #4
    New Coder
    Join Date
    Oct 2011
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts
    lol hi, nice of you to join us
    also Dan, I just set up piping to a php script so this should prove interesting

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts
    OK so I Finally actually got around to setting up the Piping and originally it was "erroring" out and when ever i'd email it. It would do the post master failure etc
    right now I have this below. The first part is what I pulled off the internet the 2nd part (after the includes) takes the parsed email and puts it in a database. Except.... its not actually putting it in the database.
    my phpDesigner is saying there as an issue on line 26 but I know its above like on line 23 the For statement. but when I try to correct it; says its incorrect.


    Do you guys have any ideas? im guessing im missing something or something is in correct
    Code:
    #/usr/bin/php -q
    <?php
    // start output buffering
    ob_start();
    // 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
    
         
       include ('includes/db_connect.php');  
    
    $con =mysql_connect($hostname, $username, $password); 
    
    mysql_select_db($db, $con);
    
    $sql="INSERT INTO Gas_Log  (Car_Miles_Total) 
    VALUES
    ('$subject')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    
    
    mysql_close($con);

  • #6
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,739
    Thanks
    41
    Thanked 191 Times in 190 Posts
    instead of doing an email service can you set up a different service? ie all you would need to do is set up an http handle that would interpret the request and do everything via that- you could have this running on a server easily and would not be too hard to code (as far as the request handle and store into the database)... your hardest 'point' I would see would be making it so you could text to the service- I know how I would do it, but my solution would be to not text and make an application that would set up a connection and initiate the request... I have not done any work with 'texting' services- I am sure there is a way to standardize it though

    essentially what I am thinking though is...
    Send http Req by some means

    on back end accept request, process data (validate and store it) and reply (or don't reply)

    the second part is quick- the first is the hurdle

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #7
    New Coder
    Join Date
    Oct 2011
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the suggestion,
    For me texting it (its not really true texting) What i do is, just text an email and it sends to that email like a regular email. That's why i was trying to get email piping to work.

    there are services out there like Twlio that will do it but thats paid, and while it is not a lot of money. Why should i do that when i can do it for free. I believe its 1 c to text and 1 dollar a month for a phone number to text it too.

    Were there any services you had in mind besides creating an app? I was thinking of creating an app as well but i am not a java programmer.

  • #8
    New Coder
    Join Date
    Oct 2011
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Good morning,
    So i am just updating this because I am having trouble with the script that I have found online. I contacted my website host because I thought it was an error in Cpanel but after gathering information they said it was functioning properly.
    When I email it , I always get a email failure and it sends it back. After researching it that sometimes happens when there is output in the script but I don't think I have any output in the script just variable that get put into a database
    Here is the code below the top half I did not create I pulled from the internet the part after the includes is mine which connects and adds to a database.


    Any suggestions ?

    Code:
    #!/usr/local/bin/php -q
    
    <?php
    chdir(dirname(__FILE__));
    $fd = fopen("php://stdin", "r");
    $email = "";
    while (!feof($fd)) {
        $email .= fread($fd, 1024);
    }
    fclose($fd);
    
    if(strlen($email)<1) {
        die(); 
    }
    
    // handle email
    $lines = explode("\n", $email);
    
    // empty vars
    $from = "";
    $to="";
    $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];
            }
            if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
                $to = $matches[1];
            }
        } else {
            // not a header, but message
            $message .= $lines[$i]."\n";
        }
        if (trim($lines[$i])=="") {
            // empty line, header section has ended
            $splittingheaders = false;
        }
    }
    
       include ('includes/db_connect.php');  
    
    $con =mysql_connect($hostname, $username, $password); 
    
    mysql_select_db($db, $con);
    
    $sql="INSERT INTO Gas_Log  (Car_Miles_Total) 
    VALUES('$message')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    
    
    mysql_close($con);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •