Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 电子邮件::MIME可以';t解析来自Gmail的消息_Perl_Email_Imap - Fatal编程技术网

Perl 电子邮件::MIME可以';t解析来自Gmail的消息

Perl 电子邮件::MIME可以';t解析来自Gmail的消息,perl,email,imap,Perl,Email,Imap,所以我使用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 = 'user

所以我使用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
如果我替换

$body = $email->body_str;

我得到输出:

Body = 
(即空字符串)


这是怎么回事?有没有一种方法可以让我获得消息的原始正文(->body\u raw也不起作用)?我可以使用regex解析出正文,Email::MIME并不是我见过的最好的文档包

body和body_str方法仅适用于单个mime部件。大多数情况下,这将是一条简单的短信。对于任何更复杂的内容,请使用parts方法获取每个mime组件,该组件本身就是一个Email::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";
}

这与gmail无关。Simple也会以同样的方式失败。你不需要使用IMAP::Simple::Gmail来阅读Gmail,除非你想使用Gmail IMAP扩展。这段代码没有。我可能应该提到,我的简单代码示例在更复杂的mime结构上会失败,例如,如果任何mime组件嵌套并包含子组件。如果您不想使用mime解析,例如,您想要完整的原始输出,请使用Email::simple代替Email::mime。Email::Simple的body方法按预期返回整个原始消息。我忘记了最后一个更改。top方法只返回标题。要获取完整消息,请将top($i)替换为get($i)。
Body = 
my @parts = $email->parts;
for my $part (@parts) {
print "type: ", $part->content_type, "\n";
print "body: ", $part->body, "\n";
}