Jump to content

Pull info from email make var.


drewtoma

Recommended Posts

I am a newbie to PHP so thanks for your time and effort on this one.So I have made a simple script(PM me for a copy) and it takes entered information and makes an invoice. What I would like to be able to do I make a script that reads my email. And all the paypal emails it scrapes specific data, then turns that into var's and then displays those var's(Like my script). Is this even possible? I will put a link to my script once I put the finishing touches on it 5~10min.Thanks again.

Link to comment
Share on other sites

Thanks, maby one day I will understand that.
Show us a sample email and maybe we can work up a solution.
Link to comment
Share on other sites

Show us a sample email and maybe we can work up a solution.
Odveously the information is excluded.PayPal logo Jul 31, 2010 13:33:48 PDTTransaction ID: *****************Hello DrDrewsDisocuntAuto,You received a payment of $108.00 USD from "Full Name" (email@hotmail.com)Thanks for using PayPal. You can now ship any items.To see all the transaction details, log in to your PayPal account.It may take a few moments for this transaction to appear in your account.Seller Protection - EligibleBuyerFull NameFull Nameemail@hotmail.com Note to sellerThe buyer hasn't sent a note.Shipping address - confirmedFull NameAddressTown, STATE ZIPCODEUnited States Shipping detailsUPS GroundDescription Unit price Qty AmountDENSO 234-9009 Fuel To Air Ratio SensorItem# 160458507391 $100.00 USD 1 $100.00 USD Shipping and handling $8.00 USDInsurance - not offered ----Total $108.00 USDPayment $108.00 USDPayment sent to drew@drdda.com Questions? Go to the Help Center at: www.paypal.com/help.Please do not reply to this email. This mailbox is not monitored and you will not receive a response. For assistance, log in to your PayPal account and click Help in the top right corner of any PayPal page.To receive email notifications in plain text instead of HTML, log in to your PayPal account, go to your Profile, and click Notifications.PayPal Email ID PP7**The email was sent in HTML so it looks different.
Link to comment
Share on other sites

Oddly enough, I just did this. My script is pretty specific to the application it's a part of though, so much of the code won't work out of my application. This also only retrieves the buyer name and email and the item number. I'll try to just post pieces of it so you can see how the parts work.This is how to connect to an email account using IMAP:

$mailbox = '{mail.domain.com:143/novalidate-cert}';$mbox = imap_open($mailbox, 'paypal_wps+domain.com', $config['db_pass']);

The first line tells it which mail server to connect to, the port, and tells it to ignore SSL errors. The second line connects to the mail server and logs into the account. Note that the @ sign is given as +, and you'll need to fill in the email password where I have the config option.I've got several business rules in my script, one of the options is to delete all mail from the account even if it wasn't processed. So it checks for the delete flag in $_GET, and then starts looping through the inbox:

$delete_all = isset($_GET['delete']);$allowed_hosts = array('paypal.com'); // only process mail sent from certain serversif (!$mbox)  echo ('Failed opening mailbox<br>' . print_r(imap_errors(), true));else{  echo "<h1>Messages in INBOX</h1>\n";  $box = imap_check($mbox);  echo 'Found ' . $box->Nmsgs . ' messages<br>';  echo '<ul>';  for ($i = 1; $i <= $box->Nmsgs; $i++)  {					   	echo '<li><ul>';	$headers = imap_headerinfo($mbox, $i);	$body = imap_body($mbox, $i);	echo '<li>Received: ' . htmlentities($headers->date) . '</li>';	echo '<li>Subject: ' . htmlentities($headers->subject) . '</li>';	echo '<li>From: ' . htmlentities($headers->fromaddress) . '</li>';	$from_paypal = false;	for ($j = 0; $j < count($headers->sender); $j++)	{	  if (isset($headers->sender[$j]->host) && in_array(strtolower($headers->sender[$j]->host), $allowed_hosts))	  {		$from_paypal = true;		break;	  }	}	if (!$from_paypal)	{	  /* echo '<li>Headers: <pre>';	  var_dump($headers);	  echo '</pre></li>'; */	  echo '<b>Skipping this email, not from known sender.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	echo '<li><pre>' . $body . '</pre></li>';  	// your other processing code	echo '</ul></li>';	imap_delete($mbox, $i);  }  echo '</ul>';  imap_expunge($mbox);  imap_close($mbox);}

That code will loop through each message and print the headers and body if the message was from paypal. Inside that loop, I also use this code to get the buyer's information and item number:

// get buyer name and email	$pattern = '#Buyer:[ \r\n]+([\w\s=\-\.\']+)[\r\n]+([\w=\-\.]+)[\r\n]+#';	$matches = array();	$num = preg_match($pattern, $body, $matches);	if ($num == 0)	{	  echo '<b>Skipping this mail, buyer information not found.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	$fullname = $matches[1];	$name_pieces = explode(' ', $fullname);	$fname = trim(array_shift($name_pieces));	$lname = trim(implode(' ', $name_pieces));	$email = trim(str_replace('=40', '@', $matches[2]));	if (empty($email))	{	  echo '<b>Skipping this mail, email address was empty.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	echo '<li>First Name: ' . $fname . '<br>Last Name: ' . $lname . '<br>Email: ' . $email . '</li>';	// get item number purchased	$pattern = '#-----[\r\n]+Description:.+, Item=23: ([\w\s]+)[\r\n]+#';	$matches = array();	$num = preg_match($pattern, $body, $matches);	if ($num == 0)	{	  echo '<b>Skipping this mail, item number not found.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	$code = trim($matches[1]);	if (empty($code))	{	  echo '<b>Skipping this mail, item number was empty.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	echo '<li>Code: ' . $code . '</li>';

That would go into the loop where it says to add your processing code. You can pull that script up in a browser to see it process the account, and if you add "?delete" after the PHP script filename then it will delete all of the messages it finds also, or else it will only delete the messages it processes. The rest of the code in my script is pretty application-specific, not relating to stuff you would want to do. I have that script scheduled to run using cron, so it checks the account every 5 minutes.

Link to comment
Share on other sites

WOW great thanks that's just what Drew needed.

Oddly enough, I just did this. My script is pretty specific to the application it's a part of though, so much of the code won't work out of my application. This also only retrieves the buyer name and email and the item number. I'll try to just post pieces of it so you can see how the parts work.This is how to connect to an email account using IMAP:
$mailbox = '{mail.domain.com:143/novalidate-cert}';$mbox = imap_open($mailbox, 'paypal_wps+domain.com', $config['db_pass']);

The first line tells it which mail server to connect to, the port, and tells it to ignore SSL errors. The second line connects to the mail server and logs into the account. Note that the @ sign is given as +, and you'll need to fill in the email password where I have the config option.I've got several business rules in my script, one of the options is to delete all mail from the account even if it wasn't processed. So it checks for the delete flag in $_GET, and then starts looping through the inbox:

$delete_all = isset($_GET['delete']);$allowed_hosts = array('paypal.com'); // only process mail sent from certain serversif (!$mbox)  echo ('Failed opening mailbox<br>' . print_r(imap_errors(), true));else{  echo "<h1>Messages in INBOX</h1>\n";  $box = imap_check($mbox);  echo 'Found ' . $box->Nmsgs . ' messages<br>';  echo '<ul>';  for ($i = 1; $i <= $box->Nmsgs; $i++)  {					   	echo '<li><ul>';	$headers = imap_headerinfo($mbox, $i);	$body = imap_body($mbox, $i);	echo '<li>Received: ' . htmlentities($headers->date) . '</li>';	echo '<li>Subject: ' . htmlentities($headers->subject) . '</li>';	echo '<li>From: ' . htmlentities($headers->fromaddress) . '</li>';	$from_paypal = false;	for ($j = 0; $j < count($headers->sender); $j++)	{	  if (isset($headers->sender[$j]->host) && in_array(strtolower($headers->sender[$j]->host), $allowed_hosts))	  {		$from_paypal = true;		break;	  }	}	if (!$from_paypal)	{	  /* echo '<li>Headers: <pre>';	  var_dump($headers);	  echo '</pre></li>'; */	  echo '<b>Skipping this email, not from known sender.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	echo '<li><pre>' . $body . '</pre></li>';  	// your other processing code	echo '</ul></li>';	imap_delete($mbox, $i);  }  echo '</ul>';  imap_expunge($mbox);  imap_close($mbox);}

That code will loop through each message and print the headers and body if the message was from paypal. Inside that loop, I also use this code to get the buyer's information and item number:

// get buyer name and email	$pattern = '#Buyer:[ \r\n]+([\w\s=\-\.\']+)[\r\n]+([\w=\-\.]+)[\r\n]+#';	$matches = array();	$num = preg_match($pattern, $body, $matches);	if ($num == 0)	{	  echo '<b>Skipping this mail, buyer information not found.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	$fullname = $matches[1];	$name_pieces = explode(' ', $fullname);	$fname = trim(array_shift($name_pieces));	$lname = trim(implode(' ', $name_pieces));	$email = trim(str_replace('=40', '@', $matches[2]));	if (empty($email))	{	  echo '<b>Skipping this mail, email address was empty.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	echo '<li>First Name: ' . $fname . '<br>Last Name: ' . $lname . '<br>Email: ' . $email . '</li>';	// get item number purchased	$pattern = '#-----[\r\n]+Description:.+, Item=23: ([\w\s]+)[\r\n]+#';	$matches = array();	$num = preg_match($pattern, $body, $matches);	if ($num == 0)	{	  echo '<b>Skipping this mail, item number not found.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	$code = trim($matches[1]);	if (empty($code))	{	  echo '<b>Skipping this mail, item number was empty.</b></ul></li>';	  if ($delete_all) imap_delete($mbox, $i);	  continue;	}	echo '<li>Code: ' . $code . '</li>';

That would go into the loop where it says to add your processing code. You can pull that script up in a browser to see it process the account, and if you add "?delete" after the PHP script filename then it will delete all of the messages it finds also, or else it will only delete the messages it processes. The rest of the code in my script is pretty application-specific, not relating to stuff you would want to do. I have that script scheduled to run using cron, so it checks the account every 5 minutes.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...