Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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.


Replies are listed 'Best First'.
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 or the mc file and re-m4 the file?

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. ( -- Feature(`local_procmail'), most major Linux distro's have it present 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.

    # 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 ask (Pilgrim) on Nov 26, 2001 at 07:12 UTC

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-05-21 13:37 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.