Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Determining who the email is meant for

by BMaximus (Chaplain)
on Nov 25, 2001 at 13:32 UTC ( #127377=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Determining who the email is meant for
Re: Determining who the email is meant for
by mitd (Curate) on Nov 25, 2001 at 14:21 UTC
    You are on the right track. The only way to be absolutely sure is to parse the MTA Envelope ie the Recieved headers. Also I noticed you left out Cc: field which may have just been a keying omission :)

    Update: merlyn point is valid if not entirely clear. Some MDA's (Mail Delivery Agents) will throw away MTA envelope information before delivery, but since you stated that you were intercepting mail from the MTA (sendmail) the envelope will be intact. Procmail has been available as an optional direct MDA for sendmail as of V8.7. (sendmail.mc -- Feature(`local_procmail'), most major Linux distro's have it present sendmail.mc though it may be commented out) and certainly is a candidate solution for your problem but not the only solution. Although local_procmail configuration has been added as a M4 feature you can actually modify the sendmail local mail delivery parameters to point to any MDA you want including a (hrumph) home rolled Perly.

    # sendmail.cf after local_promail turned on P=/usr/local/bin/procmail F=SPfhn A=procmail -Y -a $h -d $u

    mitd-Made in the Dark
    'Interactive! Paper tape is interactive!
    If you don't believe me I can show you my paper cut scars!'

Re: Determining who the email is meant for
by merlyn (Sage) on Nov 25, 2001 at 17:51 UTC
    You're looking for the "envelope to" which is not contained in any way inside the mail message in a guaranteed way, although some MTAs rewrite some of the delivery headers to include it as part of the information.

    This is the reason I worked with Joe Pruitt at Teleport to create the "procmail hack" for sendmail (now part of the distro) that permits procmail to access the "envelope to" as $1.

    But if you're looking at it already delivered in a mail box, you've lost. You'll have to get to the message further upstream.

    -- Randal L. Schwartz, Perl hacker

      I have complete control over the server ... I think :). So to use this procmail hack do I need to recompile sendmail or add something to sendmail.cf or the mc file and re-m4 the sendmail.cf file?

      BMaximus
Re: Determining who the email is meant for
by ask (Pilgrim) on Nov 26, 2001 at 07:12 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://127377]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-07-12 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls