I am trying to get some headers from an email message using an imap module but it is getting complicated. I am trying to work with a hash reference in which the keys are the header field names and the values are references to arrays of values. This is what the documentation says. I managed to capture those headers in arrays, but there is always only one element where all the info is stored. It does not matter with 'From' or 'Subject', but with 'To' it is unwanted because there is often more then one of those.So iam trying to get to this array of arrays element I solved it with splitting the string and removing leading and trailing spaces, but it's not fantastic. Could someone have a look at my script and give me some advice on how to proceed? I'll show you what i got so far:

use strict; use warnings; use Mail::IMAPClient; my $imap = Mail::IMAPClient->new( Server => 'imap.gmail.com', User => 'jim@gmail.com', Password => '*******', Ssl => 1, Debug => 0, ); die "failed to instantiate $@." unless defined $imap; $imap->select('INBOX'); my $msgcount = $imap->message_count(); print "$msgcount number of measages in your mailbox\n\n"; my @messages = $imap->messages; pop(@messages); my $msgid = pop(@messages); my $text = $imap->bodypart_string($msgid,1); my $hashref = $imap->parse_headers( $msgid, "Subject","To","From"); my @sender = @{%$hashref{"From"}}; my @recs = @{%$hashref{"To"}}; my @subject = @{%$hashref{"Subject"}}; my @rec = split(/,/, $recs[0]); print "From: $sender[0]\n"; print "Subject: $subject[0]\n\n"; foreach(@rec){ $_=~ s/^\s+|\s+$//g; print "To: $_\n"; } print $text; $imap->logout();

In reply to getting headers from essage by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.