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

I'm trying to parse mails in a perl-script, which gets the mail directly piped through from the mail-server.
I have to parse the mail, reading the From, or, if existing, the Reply-To Address (as pure as abc@def.com, not "Abc Def <abc@def.com>"), the Subject, and the body. If attachments exist, I just want to know this and strip them from the body.
How do I parse this? There are a few hundred Mail::* modules who claim to do this or that, but I don't know which to choose (even if Mail::Message looks like a candidate).
Please help, I'm just learning (how to spell) perl.

Replies are listed 'Best First'.
Re: Parse a Mail
by sch (Pilgrim) on Oct 07, 2002 at 14:07 UTC

    Hi Spida, this has come up before - if you try a few permutations on mail, email, parse in the search box at the top of the page you should see some nodes where similar questions have been asked before

    As a suggestion, have a look at this node How to parse emails with and without attachments?

      I read that, even before you suggested to.
      But the Mail::Message module looks more inviting to me, partly because it seems to be more complete and with easier access to parts of a message.
      I'm reading the mail from STDIN with
      Mail::Message->read(\*STDIN); my $absender = $msg->sender;
      That gives me an error of
      WARNING: Illegal character in field name: From test@example.com Tue Oct 8 09
      Can't call method "sender" on an undefined value at myscript.pl line (second line of code).
      What am I doing wrong?

        I can't say I've used that module, (and it looks to me as if it's more for building mail messages for sending, rather than parsing incoming messages, which is what I assume you're trying to do) but a couple of things spring to mind

        • Mail::Message can't handle STDIN - but you're obviously getting something
        • Some headers are missing - where are you getting your message from that you're passing to STDIN
        • The module doesn't like the @ - it may be that it (and other special characters) need to be escaped before passing through STDIN

        BTW, I'm not saying that it is, but you probably don't want to include real email addresses in examples on PM