Forum Moderators: coopster

Message Too Old, No Replies

extracting variables from an email pipe

         

MWpro

8:27 am on Jul 9, 2008 (gmt 0)

10+ Year Member



I am creating a script that works to replace an unreliable PayPal IPN.

Basically I am taking the "Notification of Payment Received" email that PayPal sends out after someone buys something and sending it to a script, where I want to extract the First Name, Last Name, Email Address, and Item Name.

I can divide the message into header, subject, and body. Question: how to extract these variables from the body of the email?

Sample Email


This email confirms that you have received a payment for $999.99 USD from Firstname Lastname (email@a.com).

Receipt ID: #*$!x-#*$!x-#*$!x-#*$!x

The number above is the buyer's receipt ID for this transaction. Please retain it for your records so that you will be able to reference this transaction for customer service.

--------------------------------------------------------------------------------

View the details of this transaction online
It may take a few moments for this transaction to appear in the Recent Activity list on your Account Overview.
PayPal Shopping Cart Contents

Item Name: Green Widget
Quantity: 1

Total: $999.99 USD

Cart Subtotal: $999.99 USD
Shipping: $0.01 USD
Sales Tax:
Cart Total: $1000.00 USD

Payment Details

Total amount: $1000.00 USD
Currency: U.S. Dollars
Transaction ID: 999999999999999
Buyer: Firstname Lastname


_____________________________________________

I don't even know how to approach extracting these variables... I was thinking, for instance, using a php script that pulls out the whole line beginning with "Item Name:" so that you get "Item Name: Green Widget" and then deletes the "Item Name:" part, trims the string, and sets it to a variable.

Any thoughts on how to do this?

[edited by: MWpro at 8:29 am (utc) on July 9, 2008]

rob7591

12:37 pm on Jul 9, 2008 (gmt 0)

10+ Year Member



Regular Expressions are extremely powerful and can do exactly what you're looking for.

Look up preg_match on PHP.net. I'll try to cook up a quick expression to get you started for now but you should learn about them too.

MWpro

7:59 pm on Jul 9, 2008 (gmt 0)

10+ Year Member



preg functions always intimidated me lol. An example to get started would be much appreciated.

MWpro

10:54 pm on Jul 10, 2008 (gmt 0)

10+ Year Member



bump

cameraman

7:26 am on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This needs work, but I think it would be easier to do some old-fashioned parsing before applying a regular expression:
preg_match_all('#([^:]+):([^\r^\n]+)\r\n#m',$content,$parts);
var_dump($parts);
var_dump($parts[2]);

You may not need the \r - I test regex's in a textarea which sometimes changes the rules.

Personally for this I'd just skip the regex. I'd array the lines (exlode("\n",$content)), then walk the array looking for a colon. If a line has a colon, explode that line on the colon.

[1][edited by: eelixduppy at 3:45 pm (utc) on July 11, 2008]
[edit reason] disabled smileys [/edit]

MWpro

4:52 pm on Jul 13, 2008 (gmt 0)

10+ Year Member



cameraman,

Such a simple yet smart solution, thanks so much!