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

Hi,

I have a few imap-accounts that I would like to process with a script.

Currently I am using Net::IMAP::Client and I can connect and login without problem.

However I seem to have a problem with the gmail-account.

If I do e.g.

my $msg_ids = $imap->search("ALL", "DATE");
I get an array-ref containing message-ids sorted by date on a "normal" imap-account.

But if I try this on a gmail-account I get an undef. However $imap->search("ALL") works everwhere, even with gmail.

So it seems that gmail does not like to sort.

So my question is: How can I hack around this?

Is there a way to treat gmail just like any other imap-account or will it always be a special case?

Would another library do the trick?

Many thanks!

Replies are listed 'Best First'.
Re: imap problem with gmail -- RFC 5256
by Discipulus (Canon) on Nov 03, 2017 at 08:20 UTC
    Hello morgon,

    I used the below quick program to access gmail account via IMAP. The module I use is Net::IMAP::Simple and it's docs tell us that the search in IMAP is defined in RFC 5256. The module has the capability to search but a a quick (read: no exhaustive) serach seems to suggest that gmail doesn't support RFC 5256. Anyway I confirm that the only search that runs ok with gmail is $imap->search("ALL")

    use strict; use warnings; # required modules use Net::IMAP::Simple; use Email::Simple; use IO::Socket::SSL; use HTML::Strip; use Data::Dump; # fill in your details here my $username = 'my_account_of_gmail@gmail.com'; my $password = $ARGV[0] || die "password as first argument!"; my $mailhost = 'pop.gmail.com'; # Connect my $imap = Net::IMAP::Simple->new( $mailhost, port => 993, use_ssl => 1, ) || die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n"; my $hs = HTML::Strip->new(); # Log in if ( !$imap->login( $username, $password ) ) { print STDERR "Login failed: " . $imap->errstr . "\n"; exit(64); } # Look in the the INBOX my $nm = $imap->select('INBOX'); # How many messages are there? my ($unseen, $recent, $num_messages) = $imap->status(); print "unseen: $unseen, recent: $recent, total: $num_messages\n\n"; ## Iterate through unseen messages for ( my $i = 1 ; $i <= $nm ; $i++ ) { #last if $i == 2; if ( $imap->seen($i) ) { next; } else { my $es = Email::Simple->new( join '', @{ $imap->get($i) } ); printf( "[%03d] %s\n", $i, $es->header('From'), $es->header('Subje +ct')); #print "BODY[".$es->body."]\n\n"; print "Cc-------------->", $es->header('Cc'),"\nTo------>",$es->he +ader('To'),"\n"; #dd $es; print $hs->parse( $es->body ); print +('=' x 70),"\n"; } } # Disconnect $imap->quit; exit;

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thanks for the reply.

      Yes gmail seems to do it's own thing when it comes to imap, but how do you get around it with the imap-libraries that cpan offers?

      How do you retrieve (say) the 5 newest mails programmatically?

      Does anyone know?

Re: imap problem with gmail
by RonW (Parson) on Nov 03, 2017 at 23:14 UTC
      Actually no, but thanks for the suggestion.

      Net::IMAP::Simple::Gmail is just 20 (or so) lines of code that does not do very much.

      As far as I can see all it does is using "X-GM-RAW" for searches - something that also could just as easily achieved with Net::IMAP::Simple.

      As for the sorting it is no help at all. It's just that Gmail' paradigm is searching and not sorting....

      Oh well.. it's simply a special case, regardless of the library you use.