简体   繁体   English

如何使用Perl的Mail :: MboxParser :: Mail获取电子邮件的日期?

[英]How can I get the date of an email using Perl's Mail::MboxParser::Mail?

This is a simple question. 这是一个简单的问题。 I have a little program here that reads a list of emails in a specific inbox of a user account specified by the program. 我这里有一个小程序,可以读取该程序指定的用户帐户的特定收件箱中的电子邮件列表。 I can access an account using its username, password and host. 我可以使用其用户名,密码和主机访问帐户。 The only problem is I don't know how to get the date on each of these mails. 唯一的问题是我不知道如何获取这些邮件的日期。

Here's some part of my code: 这是我的代码的一部分:

my $pop = new Mail::POP3Client(  
 USER     => $user, #some user,password & host assigned
 PASSWORD => $pass,
 HOST     => $host );

for( $i = 1; $i <= $pop->Count(); $i++ ) {

    @header  = $pop->Head($i);
    @body    = $pop->Body($i);

    $mail = new Mail::MboxParser::Mail(\@header, \@body);
    $user_email =  $mail->from()->{email

    print "Email:".$user_email; #this prints out right

    foreach( $pop->Head( $i ) ) {
            /^(Date):\s+/i && print $_, "\n";
            $date = $_;
    }
}

Now what i need is to get the only one date for each email, but that loop gives me all.. but when remove the loop, it returns an error. 现在,我需要为每封电子邮件获取唯一的日期,但是该循环为我提供了所有..但是,删除循环时,它将返回错误。 I'm using Perl. 我正在使用Perl。

Kindly help me? 请帮我吗? :) :)

According to MboxParser::Email doc, you should be able to do: 根据MboxParser :: Email文档,您应该能够执行以下操作:

$date = $mail->header->{'date'}; #Keys are all lowercase

If you have more than one date returned, $date will be an array ref and you can access the first occurence of the Date with: 如果返回的$date不止一个,则$date将是数组引用,您可以使用以下命令访问Date的第一次出现:

$date->[0];

So you shouldn't need to loop through the header and use a regular expression. 因此,您无需遍历标头并使用正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM