home *** CD-ROM | disk | FTP | other *** search
- Path: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!think.com!wupost!darwin.sura.net!gatech!purdue!spaf
- From: spaf@cs.purdue.EDU (Gene Spafford)
- Newsgroups: comp.lang.perl
- Subject: Mail handling routine
- Message-ID: <17833@ector.cs.purdue.edu>
- Date: 17 Feb 92 13:54:12 GMT
- Sender: news@cs.purdue.EDU
- Organization: Department of Computer Science, Purdue University
- Lines: 105
-
- For the heck of it, enclosed is a Perl package I use to manipulate
- mail and news articles.
-
- There are two external calls: one slurps up the header into a $
- variable, breaks up the entries (including continuation lines) into a
- @ entry, and builds an % array indexed by the header keywords.
-
- This allows you lookup entries from the header, print the unmodified
- header, or otherwise dink around with the contents.
-
- I've got about a dozen different things using this, including a mail
- server, a bunch of mailing list programs, and other stuff working with
- it for the last few months. It appears quite stable.
-
- The second routine attempts to figure out a return address based on
- the contents of the header. It uses the value of the headers (in
- order of preference): Reply-to, From, Return-path, Apparently-from.
-
- Comments, if any, welcome.
-
- # Routines to parse out an RFC 822 mailheader
- # E. H. Spafford, last mod: 11/91
- #
- # ParseMailHeader breaks out the header into an % array
- # indexed by a lower-cased keyword, e.g.
- # &ParseMailHeader(STDIN, *Array);
- # use $Array{'subject'}
- #
- # Note that some duplicate lines (like "Received:") will get joined
- # into a single entry in %Array; use @Array if you want them separate
- # $Array will contain the unprocessed header, with embedded
- # newlines
- # @Array will contain the header, one line per entry
- #
- # RetMailAddr tries to pull out the "preferred" return address
- # based on the presence or absence of various return-reply fields
-
- package MailStuff;
-
- # Call as &ParseMailHeader(FileHandle, *array)
-
- sub main'ParseMailHeader ## Public
- {
- local($save1, $save2) = ($*, $/);
- local($FH, *array) = @_;
- local ($keyw, $val);
-
- %array = ();
-
- # force unqualified filehandles into callers' package
- local($package) = caller;
- $FH =~ s/^[^']+$/$package'$&/;
-
- ($*, $/) = (1, '');
- $array = $_ = <$FH>;
- s/\n\s+/ /g;
-
- @array = split('\n');
- foreach $_ (@array)
- {
- ($keyw, $val) = m/^([^:]+):\s*(.*\S)\s*$/g;
- $keyw =~ y/A-Z/a-z/;
- if (defined($array{$keyw})) {
- $array{$keyw} .= ", $val";
- } else {
- $array{$keyw} = $val;
- }
- }
- ($*, $/) = ($save1, $save2);
- }
-
-
- # Call as $addr = &RetMailAddr(*array)
- # This assumes that the header is in RFC 822 format
-
- sub main'RetMailAddr ## Public
- {
- local(*array) = @_;
-
- local($ReplyTo) = defined($array{'reply-to'}) ?
- $array{'reply-to'} : $array{'from'};
-
- $ReplyTo = $array{'return-path'} unless $ReplyTo;
- $ReplyTo = $array{'apparently-from'} unless $ReplyTo;
-
- &CleanAddr($ReplyTo) if $ReplyTo;
- $ReplyTo;
- }
-
- sub CleanAddr ## Private
- {
- local($_) = @_;
- s/\s*\(.*\)\s*//;
- 1 while s/.*<(.*)>.*/\1/;
- s/^\s+//;
- s/\s+$//;
- $_;
- }
-
- 1;
- --
- Gene Spafford
- NSF/Purdue/U of Florida Software Engineering Research Center,
- Dept. of Computer Sciences, Purdue University, W. Lafayette IN 47907-1398
- Internet: spaf@cs.purdue.edu phone: (317) 494-7825
-
-