PHP - extracting data from mail body

Status
Not open for further replies.

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
hi.

I am looking for a smooth solution for the following problem: I wrote a php-script that is collecting emails from a mail account, reads the content of the email body (or, as it is, is supposed to read the content), extracts some of the given data and sends this data to a SOAP webservice.

the mails are in plain text format, the $body comes like this:

data1: 12345
data2: abcde
data3: xyz
END

now i want just the letters extracted to an array like $data_full = ('data2' => 'abcde', 'data3' =>'xyz');
the body contains whitespace and line-breaks as shown.

getting the mail and connecting to the SOAP ws works fine with manually given data, just how can I get the required info out of the mail body?

THANX for HELP!!
peace, bonzo
 

Hazirak

New Member
Messages
197
Reaction score
0
Points
0
Since it's plain text, you could more than likely get away with making use of the strpos() function to find out where each piece of information is, and then using substr() to grab the chunks you need... Something along the lines of this, possibly...

Code:
<?php
	//The body of the email
	$body = "data1: 12345<br />data2: abcde<br />data3: xyz<br />END";
	//Find out where "data2" begins
	$data2Start = strpos($body,"data2: ") + strlen("data2: ");
	//Find out where "data2" ends
	$data2End = strpos($body,"<br />data3: ") - (strpos($body,"data2: ") + strlen("data2: "));
	//Find out where "data3" begins
	$data3Start = strpos($body,"data3: ") + strlen("data3: ");
	//Find out where "data3" ends
	$data3End = strpos($body,"<br />END") - (strpos($body,"data3: ") + strlen("data3: "));
	//Capture the data
	$data_full = array("data2"=>substr($body,$data2Start,$data2End),"data3"=>substr($body,$data3Start,$data3End));
	//Display the captured data
	echo "<b>PROCESSED FROM:</b><br />".$body."<br />-------------------------<br />";
	echo "<b>DATA EXTRACTED FROM 'data2': </b>".$data_full["data2"]."<br />";
	echo "<b>DATA EXTRACTED FROM 'data3': </b>".$data_full["data3"];
?>
Click here to see the result

Still pretty new to PHP, so... I hope this helps. Let me know if I messed something up.
 
Last edited:

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
no messing up at all, au contraire, this is just what I needed!

thanks a lot! this saved me hours!

peace,
bonzo
Edit:
ok, here´s the final approach, which definitely solved the problem (if anyone is interested):

Code:
// first I set all the search indicators into one array, as "data1" might be //"DAta1" or "data 1" as well...

$searchterms = array("data1", "DAta1", "data 1", "data2"....);

//then I check'em all

foreach ($searchterms as $searchterm)
	{
	$nl = chr(13) . chr(10);
	$indicator = utf8_decode($searchterm);

			//Find out where data begins
			$dataStart = strpos($body, $indicator) + strlen($indicator);
			//Find out where data ends
			$dataEnd = strpos($body, $nl, $dataStart) - (strpos($body, $indicator) + strlen($indicator));
			$data = substr($body,$dataStart,$dataEnd);
			
			//Capture the data
			$data_full[$searchterm] = utf8_encode($data);
	  		
	}

as i cannot know which of the $searchterms is showing up at all and especially which one is following, the $dataEnd-strpos is checking from the next linebreak (defined as $nl) following $dataStart.

utf8_decode and utf8_encode are used because otherwise special characters used in the mail body will cause SERIOUS trouble!

peace,
bonzo
 
Last edited:
Status
Not open for further replies.
Top