Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Some time ago Gmail added IMAP support... and I thought it would be nice to see if Perl could cope with it.

Having used Mail::POP3Client and Mail::IMAPClient in the past, the latter was a natural choice for this challenge. The only thing is that Mail::IMAPClient does not support operations over SSL natively, even if it's possible to provide whatever socket we want to handle communications.

Things did not go smoothly, anyway, because I discovered that it doesn't suffice to simply create the socket and pass it to the client. After some debugging, I finally came to a working example:

#!/usr/bin/perl use strict; use warnings; use Mail::IMAPClient; use IO::Socket::SSL; # Connect to the IMAP server via SSL and get rid of server greeting me +ssage my $socket = IO::Socket::SSL->new( PeerAddr => 'imap.gmail.com', PeerPort => 993, ) or die "socket(): $@"; my $greeting = <$socket>; my ($id, $answer) = split /\s+/, $greeting; die "problems logging in: $greeting" if $answer ne 'OK'; # Build up a client attached to the SSL socket and login my $client = Mail::IMAPClient->new( Socket => $socket, User => 'youraccount', Password => 'yourpass', ) or die "new(): $@"; $client->State(Mail::IMAPClient::Connected()); $client->login() or die 'login(): ' . $client->LastError(); # Do something just to see that it's all ok print "I'm authenticated\n" if $client->IsAuthenticated(); my @folders = $client->folders(); print join("\n* ", 'Folders:', @folders), "\n"; # Say bye $client->logout();
There were two tricky parts:
  • the server sends you a greeting message that you have to consume by your own;
  • you have to explicitly put the $client in connected state.
From now on... it's a matter of RTFM!

Update: added a few comments in the code.

Update: thanks to markov, we now have some more features integrated, and using IO::Socket::SSL is more straightforward and DWIMmy:

#!/usr/bin/env perl use strict; use warnings; use Mail::IMAPClient; use IO::Socket::SSL; # Connect to the IMAP server via SSL my $socket = IO::Socket::SSL->new( PeerAddr => 'imap.gmail.com', PeerPort => 993, ) or die "socket(): $@"; # Build up a client attached to the SSL socket. # Login is automatic as usual when we provide User and Password my $client = Mail::IMAPClient->new( Socket => $socket, User => 'youraccount', Password => 'yourpass', ) or die "new(): $@"; # Do something just to see that it's all ok print "I'm authenticated\n" if $client->IsAuthenticated(); my @folders = $client->folders(); print join("\n* ", 'Folders:', @folders), "\n"; # Say bye $client->logout();

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Io ho capito... ma tu che hai detto?

In reply to Gmail IMAP access by polettix

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (10)
    As of 2015-07-03 16:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (53 votes), past polls