簡體   English   中英

使用Perl解碼MIME中的附件時出錯

[英]Error in decoding attachment in MIME using perl

我在perl中編寫了一個腳本,以使多段MIME消息附加圖像,這是腳本

use MIME::Parser;
use FileHandle;

$ffh = FileHandle->new;
if ( $ffh->open(">m2.txt") ) {

    #print <$fh>;
}

### Create an entity:
$top = MIME::Entity->build(
    From    => 'me@myhost.com',
    To      => 'you@yourhost.com',
    Subject => "Hello, nurse!",
    Data    => "How are you today"
);

### Attach stuff to it:
$top->attach(
    Path     => "im.jpg",
    Type     => "image/jpg",
    Encoding => "base64"
);

### Output it:
$top->print($ffh);

之后,我嘗試使用以下代碼解析上述腳本生成的輸出消息

use MIME::Parser;
use FileHandle;

$fh = FileHandle->new;
if ( $fh->open("<m2.txt") ) {

    #print <$fh>;
}

### Create parser, and set some parsing options:
my $parser = new MIME::Parser;
$parser->output_to_core(1);

### Parse input:
$entity = $parser->parse($fh) or die "parse failed\n";

print $entity->head->get('subject');
print $entity->head->get('from');

print $entity->head->get('to');
print $entity->head->get('cc');
print $entity->head->get('date');
print $entity->head->get('content-type');

my $parts = $entity->parts(1);
my $body  = $parts->bodyhandle;
print $parts->head->get('content-type');

$ffh = FileHandle->new;
if ( $ffh->open(">C:/Users/Aamer/Desktop/im.jpg") ) {
    $body->print($ffh);
}

現在,每件事都能正確解析並返回正確的值,除了作為附件的輸出圖像外,該圖像有些損壞,我嘗試十六進制比較它們,提取的圖像和原始圖像之間有些區別,有人可以告訴我這是怎么回事嗎? 謝謝

您的路徑名表示您在Windows上,默認情況下,Perl在其中以文本模式打開文件。 這意味着在寫入文件時,它將圖像中每次出現的0x0A(LF)轉換為0x0D 0x0A(CRLF),從而損壞了圖像。

以二進制模式打開文件:

$ffh->open("C:/Users/Aamer/Desktop/im.jpg", "wb")

您是否在附加文件之前關閉文件句柄? 可能是一個緩沖問題。 關閉文件句柄會將數據刷新到文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM