如何使用 MIME::Parser 从邮件中获取正文部分?
use MIME::Parser;
use Data::Dumper qw(Dumper);
$parser = MIME::Parser->new( );
$parser->output_to_core(1); # don't write attachments to disk
while (<STDIN>) {
$MESSAGE .= $_;
}
$message = $parser->parse_data($MESSAGE); # die( )s if can't parse
$head = $message->head( ); # object--see docs
$preamble = $message->preamble; # ref to array of lines
$epilogue = $message->epilogue; # ref to array of lines
$num_parts = $message->parts;
for (my $i=0; $i < $num_parts; $i++) {
print "part number = $i\n";
my $part = $message->parts(1);
my $content_type = $part->mime_type;
my $body = $part->as_string;
print $body;
}
在输出中我也可以看到内容标题。我们是否有任何流程可以仅将消息正文内容收集到数组中?
提前致谢。
use MIME::Parser;
use Data::Dumper qw(Dumper);
$parser = MIME::Parser->new( );
$parser->output_to_core(1); # don't write attachments to disk
while (<STDIN>) {
$MESSAGE .= $_;
}
$message = $parser->parse_data($MESSAGE); # die( )s if can't parse
$head = $message->head( ); # object--see docs
$preamble = $message->preamble; # ref to array of lines
$epilogue = $message->epilogue; # ref to array of lines
$num_parts = $message->parts;
for (my $i=0; $i < $num_parts; $i++) {
print "part number = $i\n";
my $part = $message->parts(1);
my $content_type = $part->mime_type;
my $body = $part->as_string;
print $body;
}
In the output i can see content headers as well. Do we have any process to have only the message body content collecteed into array?
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
my $body = $part->as_string;
是错误的。 MIME 部分($part
是类 MIME::Entity) 由标题、正文和尾声组成。这应该是
my @body_encoded_lines = $part->body
(行列表)或my $body_decoded_handle = $part->bodyhandle
(MIME::Body)。my $body = $part->as_string;
is wrong. A MIME part ($part
is an instance of class MIME::Entity) is comprised of headers and body and epilogue.This should instead be either
my @body_encoded_lines = $part->body
(list of lines) ormy $body_decoded_handle = $part->bodyhandle
(instance of MIME::Body).