gpt4 book ai didi

Perl:Gmail 的 Mail::IMAPClient 不阅读新邮件

转载 作者:行者123 更新时间:2023-12-02 01:54:44 25 4
gpt4 key购买 nike

经过数小时的搜索、阅读和寻找答案,我来到这里寻求帮助:

我正在尝试编写一个客户端来使用 Perl 模块阅读我的 Gmail Mail::IMAPClient .到目前为止一切正常,而且效果很好,但是当我尝试获取“收件箱”文件夹中的电子邮件数量时,它没有给出正确的数字:

# 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";

从我的“收件箱”文件夹中读取时,将 @msgIDs 分配给命令:

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

当实际上有 1149 时,所有这些都会导致相同的数字被认为是收件箱中的消息数(准确地说是 1194)。打印消息数量后,程序继续询问用户他们希望看到多少最近消息的“标题”(这意味着如果我输入“5”,我会看到“主题”和“发件人”来自五个最新消息的 header 字段)。此代码紧跟在前面显示的代码之后:

# 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);

&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 的“主题”和“发件人” header 字段。因此,当我调用 &read_more(scalar(@msgIDs), $read_num) 时,我打算查看 $read_num 最新消息的 header 字段。相反,我没有看到我的 9 条最新消息的任何标题字段,即使我能够在程序中完美地阅读它们(我没有显示代码;它会使事情复杂化)。找到的消息数不会改变。如果我收到一条新消息,那么我将无法看到最新的 10 条消息。客户端卡在邮件 ID 1193。我已经将我的 Gmail 设置配置为允许 IMAP。

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

最佳答案

您认为@msgID 是一个从 0 开始到 message_count-1 结束的序列。不必如此。例如,我当前有一条消息,但这条消息的 msgID 是 6。因此,您应该使用搜索给出的 msgID,而不是假设只是一个简单的序列。

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

关于Perl:Gmail 的 Mail::IMAPClient 不阅读新邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20910876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com