I wrote a script in perl to make multipart MIME message attaching an image here is the script
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);
after that I tried to parse the generated output message from the above script using the following code
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);
}
now every thing parse correctly and returned the values right except for the output image as attachment the image some how is corrupted I tried to hex compare them there were some difference between extracted image and the original image can any one tell me what's wrong here ? Thanks
Your pathname indicates you're on Windows, where Perl opens files in text mode by default. That means when writing a file it converts every occurrence of 0x0A (LF) in your image to 0x0D 0x0A (CRLF), corrupting your image.
Open the file in binary mode:
$ffh->open("C:/Users/Aamer/Desktop/im.jpg", "wb")