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

Hello fellow monks, I'm writing a script to handle e-mail input. Thre are basically two parts to my script. One part is a loop, like this:
until ($FromAddress =~ /From/i) { $FromAddress = <STDIN> }
This part gets me a 'From' address that I can use to search a database of authorized users. Then, some time later, I do this:
my $parser = new MIME::Parser; my $entity = $parser->parse(\*STDIN) or debug_parser();
Now, the problem is this. When I take a message in a text file and pipe it to my script, everything works just fine. However, when I pipe it from a mail server alias, the MIME::Parser never gets any data. The From: checker gets the data fine. To check the alias, I changed it to pipe to the command "cat - > some.file". The entire message, including MIME data, was found in some.file. In fact, when I turned around and did "cat some.file | perl MyCommand.pl", everything worked fine! But no matter what I do, nothing ever gets to the Mime Parser. I can tell this by examining the directory that Mime::Parser creates: the message text is 0 bytes and no attachments get detached. I know this is not a file permission error because, when I change file permissions so that the output directory is not writeable, I get a bounce message from my mail server. I'm using PostFix. Anyone know what might be going on? Thanks, SM

Replies are listed 'Best First'.
Re: Odd MIME::Parser STDIN behavior
by tedrek (Pilgrim) on Aug 07, 2003 at 03:37 UTC

    It seems a little unclear on what exactly is going wrong. The way I read this is you are getting the email on STDIN from the mail server, and you can get it on STDIN from `cat` but MIME::Parser doesn't give a message back *ever*. So if that interpretation is wrong anything further I have to say may be baseless :)

    Your first loop reads in the message up to and including the From: line, then MIME::Parser starts at the line *after* 'From:'. Any number of headers (including all headers) could have been discarded by that point so the entity from MIME::Parser won't be the complete message, you probably want to start by parsing the message then getting the From header directly from $entity. I'd also like to point out that From headers can be easily forged so relying on them for security would seem flawed.