Closed Thread
Results 1 to 4 of 4

Thread: PHP - extracting data from mail body

  1. #1
    bonzo meier is offline x10Hosting Member bonzo meier is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    47

    PHP - extracting data from mail body

    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

  2. #2
    vitallybankou14 is offline x10Hosting Member vitallybankou14 is an unknown quantity at this point
    Join Date
    Dec 2007
    Posts
    2

    Re: PHP - extracting data from mail body

    that's interesting

  3. #3
    Hazirak's Avatar
    Hazirak is offline x10 Sophmore Hazirak is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Georgia
    Posts
    197

    Re: PHP - extracting data from mail body

    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 by Hazirak; 01-12-2008 at 04:49 AM.

  4. #4
    bonzo meier is offline x10Hosting Member bonzo meier is an unknown quantity at this point
    Join Date
    Jan 2008
    Posts
    47

    Re: PHP - extracting data from mail body

    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 by bonzo meier; 01-15-2008 at 08:55 AM. Reason: Automerged Doublepost

Closed Thread

Similar Threads

  1. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  2. PHP mail() - AGAIN!
    By epoclaen in forum Free Hosting
    Replies: 16
    Last Post: 11-28-2007, 10:58 AM
  3. forums?
    By twin2t7 in forum Free Hosting
    Replies: 17
    Last Post: 11-20-2007, 03:08 PM
  4. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers