tags:

views:

100

answers:

1

This is a simple question. I have a little program here that reads a list of emails in a specific inbox of a user account specified by the program. I can access an account using its username, password and host. The only problem is I don't know how to get the date on each of these mails.

Here's some part of my code:

my $pop = new Mail::POP3Client(  
 USER     => $user, #some user,password & host assigned
 PASSWORD => $pass,
 HOST     => $host );

for( $i = 1; $i <= $pop->Count(); $i++ ) {

    @header  = $pop->Head($i);
    @body    = $pop->Body($i);

    $mail = new Mail::MboxParser::Mail(\@header, \@body);
    $user_email =  $mail->from()->{email

    print "Email:".$user_email; #this prints out right

    foreach( $pop->Head( $i ) ) {
            /^(Date):\s+/i && print $_, "\n";
            $date = $_;
    }
}

Now what i need is to get the only one date for each email, but that loop gives me all.. but when remove the loop, it returns an error. I'm using Perl.

Kindly help me? :)

+1  A: 

According to MboxParser::Email doc, you should be able to do:

$date = $mail->header->{'date'}; #Keys are all lowercase

If you have more than one date returned, $date will be an array ref and you can access the first occurence of the Date with:

$date->[0];

So you shouldn't need to loop through the header and use a regular expression.

ccheneson