http://www.perlmonks.org?node_id=127377

BMaximus has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a program that parses an incoming email through a pipe from sendmail. It needs to determine who the email was really sent to since its not guaranteed that the To: field is set to the address that the mail was sent to. Most list servers fill the To: field with the list address. So if the recipient isn't in the To,Apparently-To or the X-Apparently-To fields. How am I to determine who the email was really sent to? Barring that the program looks for a specific domain, the one that the email was sent to makes the job easier.

My take on it is as follows but I'm sure there's a better way.

use Mail::Header to parse out the header of the incoming mail. Look in the To: field and see if the domain matches what we're looking for. If its not, then look in the other fields. If they're not then use Email::Find to parse out the Received field from theMail::Header object and go through the email addresses it returns for a valid one.

BMaximus