Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2023-02-04 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?