使用Mail :: POP3Client访问Gmail

时间:2012-12-17 19:49:12

标签: perl email gmail

我正在使用此脚本从gmail帐户获取所有邮件:

#!/usr/bin/perl

use Mail::POP3Client;
use IO::Socket::SSL;
no warnings;

my $username = 'username';
my $password = 'password';
my $mailhost = 'pop.gmail.com';
my $port = 995;

my $socket = IO::Socket::SSL->new(
                PeerAddr => 'pop.gmail.com',
                PeerPort => 995,
                Proto    => 'tcp',
            )
            or die "No socket!: $!\n";
my $pop = Mail::POP3Client->new();
$pop->User($username);
$pop->Pass($password);
$pop->Socket($socket);
$pop->Connect();

# me fijo cuantos hay
my $count = $pop->Count();
my $size = $pop->Size();

print "count[$count]\n";

在Gmail帐户中有大约1.500条消息...但总是$ pop-> Count()返回250或更多..从不1.500。

任何人都知道这件事吗?

提前致谢。

1 个答案:

答案 0 :(得分:2)

最后,我使用IMAP代替POP。

#!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
use IO::Socket::SSL;

my $socket = IO::Socket::SSL->new(
   PeerAddr => 'imap.gmail.com',
   PeerPort => 993,
  )
  or die "socket(): $@";

my $client = Mail::IMAPClient->new(
   Socket   => $socket,
   User     => 'username',
   Password => 'password',
  )
  or die "new(): $@";

my $cont = 1;
$client->select('INBOX');
my @mails = ($client->seen(),$client->unseen);
foreach my $id (@mails) {
    my $from = $client->get_header($id, 'From');
    if ($from =~ /([a-zA-Z\_\-\.0-9]+@[a-zA-Z\_\-0-9]+\.[0-9a-zA-Z\.\-\_]+)/) {
        my $email = lc $1;
        print "email[$email]\n";
    };
};

$client->logout();

这项工作很棒。