Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: getting headers from essage
by LanX (Saint) on Dec 16, 2025 at 19:38 UTC | |
by Anonymous Monk on Dec 16, 2025 at 20:06 UTC | |
by ikegami (Patriarch) on Dec 16, 2025 at 20:30 UTC | |
by LanX (Saint) on Dec 16, 2025 at 20:44 UTC | |
by ikegami (Patriarch) on Dec 16, 2025 at 20:50 UTC | |
| |
|
Re: getting headers from essage
by ikegami (Patriarch) on Dec 16, 2025 at 20:41 UTC | |
|
Re: getting headers from essage
by LanX (Saint) on Dec 16, 2025 at 21:16 UTC | |
by ikegami (Patriarch) on Dec 16, 2025 at 21:31 UTC | |
by LanX (Saint) on Dec 16, 2025 at 22:01 UTC | |
|
Re: getting headers from essage
by Anonymous Monk on Dec 17, 2025 at 12:16 UTC | |
by LanX (Saint) on Dec 17, 2025 at 12:34 UTC |