Perl:Mail :: IMAPClient with Gmail无法读取新邮件

时间:2014-01-03 19:01:32

标签: perl email gmail imap

经过几个小时的搜索,阅读和抓答,我来这里寻求帮助:

我正在尝试用Perl模块编写客户端来阅读我的Gmail {{3 }}。到目前为止,所有功能都很实用,效果很好,但是当我尝试在“INBOX”文件夹中获取电子邮件数量时,它没有给出正确的数字:

# initialize the IMAP object
$imap = Mail::IMAPClient->new ( Server => 'imap.gmail.com',
                                User => $username,
                                Password => $password,
                                Port => 993,
                                Ssl => 1,
                                Uid => 1 )
or die "Could not connect to server, terminating...\n";

# find which folder to read from
print "Mailboxes: ".  join(", ", $imap->folders) . "\n";
print "Folder to use: ";
chomp (my $folder = <STDIN>);
$imap->select($folder) or die "select() failed, terminating...\n";

# get message IDs and number of messages
my @msgIDs = $imap->search("ALL");
print scalar(@msgIDs) . " message(s) found in mailbox.\n";

从我的“INBOX”文件夹中读取时,为命令分配@msgIDs

$imap->search("ALL");
$imap->messages;
$imap->message_count;

当实际存在1194时,所有结果都会产生相同的数字,即收件箱中的邮件数量(确切地说是1149)。 在打印消息数量之后,程序继续向用户询问他们想要看到的最近消息的“标题”数量(这意味着如果我输入“5”,我会看到“主题”和“来自”来自五个最新消息的标题字段)。此代码紧跟在之前显示的代码之后:

# get number of messages to read (so the entire inbox isn't looked at)
print "Read how many? ";
chomp (my $read_num = <STDIN>);

# read the original number of headers requested
&read_more (scalar(@msgIDs), $read_num);

&amp; read_more()子例程使用一个或两个参数,但这里是两个参数版本:

if (@_ == 2) {
        # if an empty string was passed
        if ( $_[1] eq "" ) {
                $_[1] = 0;
        }

        # print $_[1] headers behind $_[0]
        foreach ( ($_[0]-$_[1])..($_[0]-1) ) {

                my $from = $imap->get_header ($_, "from");
                my $subject = $imap->get_header ($_, "subject");

                (printf "%d: %s\n", $_, $from);
                (printf "%d: %s\n\n", $_, $subject);
         }
}

因此,如果我拨打&read_more(1000, 5),我会看到消息ID 990-999的“主题”和“发件人”标题字段。因此,当我致电&read_more(scalar(@msgIDs), $read_num)时,我打算查看$read_num最新消息的标头字段。相反,我没有看到我的9条最新消息的任何标题字段,即使我能够在程序中完全正确地读取它们(我没有显示代码;它会使事情变得复杂)。找到的邮件数量不会更改。如果我收到一条新消息,那么我将无法看到10条最新消息。客户端卡在消息ID 1193.我已将Gmail设置配置为允许IMAP。

这是我的代码中的错误,还是我的Gmail配置或其他问题?

2 个答案:

答案 0 :(得分:2)

您认为@msgID是一个以0开头并以message_count-1结尾的序列。这不一定是这种情况。例如,我当前有一条消息,但是这条消息的msgID是6.因此你应该使用搜索给出的msgID,而不是只假设一个简单的序列。

编辑:代码在构造函数中使用Uid =&gt; 1,因此搜索返回UID和get_header expectets UID。将其更改为Uid =&gt; 0使其与序列号一起使用。

答案 1 :(得分:0)

我自己修好了。好的,这需要一些错误的逻辑(无论如何,这个程序是供个人使用的,所以这没关系)。
如果电子邮件的标题中没有“发件人”标题字段(即,如果发现没有作者的电子邮件),则电子邮件不是电子邮件。因此,可以计算电子邮件直到找到空的“发件人”标题字段的子例程将正确计算收件箱中的电子邮件数量:

# the only argument is the IMAP object, that allows communication
sub message_num {

        # there's always going to be at least this many messages, so guess here
        my @guess = $_[0]->search("ALL");

        # get the guess in scalar format so we can count with it
        my $i = scalar (@guess);

        # while the "from" headers are defined
        while ( defined ($_[0]->get_header ($i, "from")) ) {
                # count the messages past what the guess said
                $i++;
        }

        # return that count
        $i;
}

因此,在我的代码中调用&message_num ($imap)会给我一些可用的消息。这是不正确的,但它允许我查看我最近的所有消息,这就是我想要的。