Extract email from returned mail using Mail.app

I’ve tried searching the forums to no avail and I really don’t know my ass from my forehead when it comes to AppleScript. Nonetheless, if anyone can help, I’d greatly appreciate it.

I’m looking for a way to use AppleScript to look at incoming emails (ones that are returned, couldn’t be delivered, etc) and extract the original recipients email address. Once the address is found, send the email to a PHP script (which will then call another script on another server).

Thanks in advance,
Kyle

I’ve come up with this bit of code thus far based upon other scripts I’ve seen. It doesn’t seem to be working, so if someone could help me out, that would be great.

using terms from application “Mail”
on perform mail action with messages theMessages for rule theRule
tell application “Mail”
try
set theRuleName to name of theRule
end try
repeat with eachMessage in theMessages
set theEmail to extract address from theBody
try
do shell script “/usr/local/bin/php -r /Users/kyle/Scripts/bounce_email.php?email=theEmail”
on error
display dialog “Couldn’t run PHP script”
end try
end repeat
end tell
end perform mail action with messages
end using terms from

Anyone? It seems like a simple thing, I just can’t figure it out since I’m not really familiar with AppleScript.