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

I am trying to write a script that parses email headers. One of the more important header types I need to process are 'Received' headers. However, when I use Mail::Message->get('received') the result is absolutely nothing. It is as if there are no 'Received' headers which is not the case. I tried looking at the code but I am not nearly experienced enough with perl to figure out what is going on. I can't even see where in the 'get' function, it does anything with headers.

I tried posting on the CPAN forum for this module but apparently no one reads it as I have the first and only post and have not gotten any response in 2 weeks. Is someone willing to look at the code to help me figure out why it is not working for 'Received' headers? TIA.

Replies are listed 'Best First'.
Re: Mail::Message->get('received') failing
by Fletch (Bishop) on Nov 12, 2008 at 16:37 UTC

    Don't ask to ask, just ask. Post the code here which you're having problems with and you'll be much more likely to get help. See also How (Not) To Ask A Question.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      I think you misunderstood my request. I was referring to the code in Mail::Message for 'get'. Do you really want me to post all that source? My code is essentially the one line call in the subject field.

        No, you need to post a trimmed down relevant sample that shows the same behavior. Maybe you've got a missing letter in a variable name and aren't using strict so it's not getting caught. Maybe you've neglected to append a newline to a print and you're suffering from buffering. Maybe you've called something that's causing a real problem down inside Mail::Message. No one here can diagnose that because you haven't shown the code that's not working. You've given no details, just a vague "something resembling this snippet of code in some context at some time isn't working for me".

        Not that it's inconceivable that there's a problem with the module in question, but no one's going to be able to divine what might be going wrong without any actual real code exhibiting the behavior in question to paw over and figure out what might be going wrong.

        Update: Something like this (which by the way works fine for me; sample output after the __END__):

        #!/usr/local/bin/perl5.10.0 use strict; use warnings; use feature ':5.10'; use Mail::Box::Manager; my $mgr = Mail::Box::Manager->new(); my $folder = $mgr->open( "$ENV{HOME}/Mail/outgoing" ); my $msg = $folder->message( 0 ); for my $header ( qw( subject from received bogus ) ) { say "Msg $header: ", $msg->get( $header ) // 'UNDEF'; } exit 0; __END__ Msg subject: testing comp Msg from: Mike Fletcher <REDACTED> Msg received: UNDEF Msg bogus: UNDEF

        That's actual code that someone can grab, tweak the path to a mailbox for their environment, and attempt to duplicate your problem. It shows exactly what is being done when things don't behave (or are behaving as it does in this case since that message has no "Received" headers). Ideally you'd even include a sample message/mbox file which includes a "Received" header but the same code doesn't print it out (but that might be getting greedy :).

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Re: Mail::Message->get('received') failing
by gone2015 (Deacon) on Nov 12, 2008 at 17:36 UTC

    Could this be relevant, from the documentation:

    $obj->get(FIELDNAME)

    Returns the value which is stored in the header field with the specified name. The FIELDNAME is case insensitive. The unfolded body of the field is returned, stripped from any attributes. See Mail::Message::Field::body().

    If the field has multiple appearances in the header, only the last instance is returned. If you need more complex handing of fields, then call Mail::Message::Head::get() yourself. See study() when you want to be smart, doing the better (but slower) job.

    I note that the field name is case insensitive, so it's not that. While this says that $msg->get('Received') will only receive the last if there are many, it doesn't quite explain why you get none at all. But I note the reference to Mail::Message::Head::get(). I also found:

    my @rec = $msg->head->study('Received');
    given as an example, and apparently should return all the received lines.