...

View Full Version : email parsing



david.ginn524
04-04-2012, 05:54 PM
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.

DanInMa
04-04-2012, 06:39 PM
Im not a php guy but your question makes me think :).

I found this, perhaps it might help?

http://ericlondon.com/configuring-server-parse-email-php-script
http://psoug.org/snippet.htm/Automatically_process_email_157.htm

david.ginn524
04-05-2012, 02:52 PM
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

david.ginn524
04-05-2012, 05:13 PM
lol hi, nice of you to join us
also Dan, I just set up piping to a php script so this should prove interesting

david.ginn524
04-26-2012, 06:41 PM
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


#/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);

alykins
04-26-2012, 08:58 PM
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

david.ginn524
04-27-2012, 02:13 PM
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. :(

david.ginn524
05-08-2012, 03:55 PM
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 ?



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum