【问题标题】:How can I convert messages in an mbox file to UTF-8?如何将 mbox 文件中的消息转换为 UTF-8?
【发布时间】:2010-09-27 03:06:28
【问题描述】:

我正在尝试修改以下程序以确保使用 Encode::decode() 将每个 msg 转换为 utf-8,但我不确定如何以及在何处放置它以使其工作。

#!/usr/bin/perl
use warnings;
use strict;
use Mail::Box::Manager;

open (MYFILE, '>>data.txt');
binmode(MYFILE, ':encoding(UTF-8)');


my $file = shift || $ENV{MAIL};
my $mgr = Mail::Box::Manager->new(
    access          => 'r',
);

my $folder = $mgr->open( folder => $file )
or die "$file: Unable to open: $!\n";

for my $msg ( sort { $a->timestamp <=> $b->timestamp } $folder->messages)
{
    my $to          = join( ', ', map { $_->format } $msg->to );
    my $from        = join( ', ', map { $_->format } $msg->from );
    my $date        = localtime( $msg->timestamp );
    my $subject     = $msg->subject;
    my $body        = $msg->decoded->string;

    # Strip all quoted text
    $body =~ s/^>.*$//msg;

    print MYFILE <<"";
From: $from
To: $to
Date: $date
Subject: $subject
\n
$body

}

【问题讨论】:

  • 我认为如果你把这个“问题”分解成各个部分,对我们所有人来说都会容易得多。
  • 好的,我会的,不过我担心每次都粘贴代码。
  • 当问题是如何实现换行时,无需粘贴任何代码。无需粘贴完整的脚本来了解如何处理附件。
  • 我希望如果人们选择提供示例,它会对他们有所帮助。我对此很陌生,但今天需要这样做,所以我要积极主动。
  • 您发布的代码越长,您期望得到的答案就越少。

标签: perl email encoding utf-8


【解决方案1】:

从我怀疑你想要替换的文档中

my $body        = $msg->decoded->string;

my $body        = $msg->decoded('UTF-8')->string;

虽然我不完全确定,也可能根本不重要。

【讨论】:

    【解决方案2】:

    脚本中似乎没有指定您希望输入采用哪种编码...通常这很重要,因为自动检测硬字符编码(编码库通常不支持)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 2018-04-01
      • 2015-04-07
      • 2015-04-13
      • 2012-06-01
      • 2013-09-12
      • 2023-03-16
      • 2017-12-08
      相关资源
      最近更新 更多