I am in the process of working on a perl script that will connect to Gmail and download all the emails. Do keep in mind that I’m only concentrating on connecting to Gmail at the moment, so the script is only set up to cycle through some folders for now. This is what I have so far:
#!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
use IO::Socket::SSL;
# Connect to IMAP via SSL and get rid of greeting message
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 loggin in: $greeting" if $answer ne 'OK';
# Build a client attached to the SSL Socket and login
my $client = Mail::IMAPClient->new(
Socket => $socket,
User => 'mail@gmail.com',
Password => 'password',
Port => 993,
)
or die "new(): $@";
my ($code, $output) = ("","");
until( $code ) {
$output = $client->_read_line or return undef;
for my $o (@$output) {
$client->_debug("Connect: Recieved this from readline: ".join("/",@$o)."\n");
$client->_reord($client->Count,$o);
next unless $o->[Mail::IMAPClient::TYPE] eq "OUTPU";
($code) = $o->[Mail::IMAPClient::DATA] =~ /^\*\s+(OK|BAD|NO)/i ;
}
}
if($code =~ /BYE|NO /) {
$client->State("Unconnected");
die "IMAP server disconnected";
}
$client->login;
print "1";
$client->select('INBOX');
my @mails = ($client->seen(),$client->unseen);
foreach my $id (@mails) { print "$id\n"; }
# Terminate the connection with IMAP
$client->logout();
When I run the script, however, the program only sits with a blinking cursor for a long period of time. It returns absolutely no output or errors. Anyone have any suggestions?
It might be easier to use a module that does the IMAP with SSL for you.
I have used both Net::IMAP::Simple::SSL and Mail::Cclient to connect to IMAP servers over SSL, although I haven’t tried to connect to Gmail.
Also, I haven’t used Mail::IMAPClient, which you are using, but it does appear to support SSL, and mentions Gmail several times in the documentation, so it should work without having to create your own SSL sockets.