0

所以我使用 PERL 和 Email::MIME 从 gmail 获取电子邮件。这是我的代码:

use Net::IMAP::Simple::Gmail;
use Email::Mime;


# Creat the object that will read the emails
$server = 'imap.gmail.com';
$imap = Net::IMAP::Simple::Gmail->new($server);


# User and password
$user = 'username@gmail.com';
$password = 'passowrd';

$imap->login($user => $password);

# Select the INBOX and returns the number of messages
$numberOfMessages = $imap->select('INBOX');

# Now let's go through the messages from the top

for ($i = 1; $i <= $numberOfMessages; $i++)
{
        $top = $imap->top($i);
    print "top = $top\n";

    $email = Email::MIME->new( join '', @{ $imap->top($i) } );
    $body = $email->body_str;
    print "Body = $body\n";
}#end for i

当我运行它时,我收到以下错误:

can't get body as a string for multipart/related; boundary="----=_Part_6796768_17893472.1369009276778"; type="text/html" at /Library/Perl/5.8.8/Email/Mime.pm line 341
Email::MIME::body_str('Email::MIME=HASH(0x87afb4)') called at readPhoneEmailFeed.pl line 37

如果我更换

$body = $email->body_str;

$body = $email->body;

我得到输出:

Body = 

(即空字符串)

这里发生了什么?有没有办法让我得到消息的原始正文(->body_raw 也不起作用)?我可以使用正则表达式解析身体

4

1 回答 1

0

Email::MIME 不是我见过的最好的文档包。

body 和 body_str 方法仅适用于单个 mime 部分。大多数情况下,这将是一条简单的短信。对于更复杂的事情,使用parts 方法来获取每个本身就是一个Email::MIME 对象的mime 组件。body 和 body_str 方法应该可以解决这个问题。html 格式的消息通常有两个 MIME 部分:text/plain 和 text/html。

这并不完全是您想要的,但应该足以向您展示发生了什么。

my @parts = $email->parts;
for my $part (@parts) {
print "type: ", $part->content_type, "\n";
print "body: ", $part->body, "\n";
}
于 2013-05-26T21:02:48.257 回答