problems with decoding: resulting image is corrupt

Discussion in 'Perl' started by Bjorn Jensen, Mar 21, 2005.

  1. Bjorn Jensen

    Bjorn Jensen Guest

    Hello!
    My resulting image har nearly same size of original (but not exact) and the
    nu.jpg can be shown,
    but it is modified so much, that it look totally different.... why? what to
    do?
    Greetings Bjorn!

    ============================
    use strict;
    use Net::pOP3;
    use MIME::Base64;

    my $username = 'user1';
    my $password = 'userpw';
    my $pop = Net::pOP3->new('mailme.org', Timeout => 60);

    if ($pop->login($username, $password) > 0) {
    my $msgnums = $pop->list; # hashref of msgnum => size
    foreach my $msgnum (keys %$msgnums) {
    my $mailfile = 'mail'.$msgnum.".txt";
    open(M, ">$mailfile") or die "Error\tCouldn't open $mailfile, $!";
    print "msg $msgnum\n";
    my $msg = $pop->get($msgnum);
    #print @$msg;
    print M @$msg;
    #$pop->delete($msgnum);
    close(M);
    }
    }

    $pop->quit;
    my $b64str;
    open(UD, ">nu.jpg") or die "$!";

    open(IND, "<mail2.txt") or die "$!";
    my $part2decode = 0;
    my $line = 0;

    while(<IND>) {
    if ( /Content-Disposition/i ) {
    $part2decode++;
    }
    if ( $part2decode ) {
    $line++;
    }
    if ( /Boundary/ ) {
    $part2decode = 0;
    }
    if ( $part2decode && $line > 1 ) {
    my $encoded = $_;

    my $decoded = decode_base64($encoded);
    print UD $decoded;
    }
    }
    close(UD);
    close(IND);
    Bjorn Jensen, Mar 21, 2005
    #1
    1. Advertising

  2. Bjorn Jensen

    Joe Smith Guest

    Bjorn Jensen wrote:

    > My resulting image har nearly same size of original (but not exact) and the
    > nu.jpg can be shown,
    > but it is modified so much, that it look totally different.... why?


    You have to use binmode() when dealing with binary files.

    > open(UD, ">nu.jpg") or die "$!";


    open(UD, '>', 'nu.jpg') or die "Cannot create nu.jpg: $!";
    binmode(UD);

    -Joe
    Joe Smith, Mar 22, 2005
    #2
    1. Advertising

  3. Bjorn Jensen

    Bjorn Jensen Guest

    Hi again!
    Oh, of course - I tried binary(UD) yesterday but this gave error.'
    Now it works - thanks!
    Now I'll look for a better way to extract attachments.
    I try to figure out, how to use
    Email::MIME::Attachment::Stripper;
    but have no success...
    The documentation is spare.
    Any good example, that I can use for expanding my case below?

    Greetings Bjørn

    "Bjorn Jensen" <> wrote in message
    news:423f4850$0$13726$...
    > Hello!
    > My resulting image har nearly same size of original (but not exact) and

    the
    > nu.jpg can be shown,
    > but it is modified so much, that it look totally different.... why? what

    to
    > do?
    > Greetings Bjorn!
    >
    > ============================
    > use strict;
    > use Net::pOP3;
    > use MIME::Base64;
    >
    > my $username = 'user1';
    > my $password = 'userpw';
    > my $pop = Net::pOP3->new('mailme.org', Timeout => 60);
    >
    > if ($pop->login($username, $password) > 0) {
    > my $msgnums = $pop->list; # hashref of msgnum => size
    > foreach my $msgnum (keys %$msgnums) {
    > my $mailfile = 'mail'.$msgnum.".txt";
    > open(M, ">$mailfile") or die "Error\tCouldn't open $mailfile, $!";
    > print "msg $msgnum\n";
    > my $msg = $pop->get($msgnum);
    > #print @$msg;
    > print M @$msg;
    > #$pop->delete($msgnum);
    > close(M);
    > }
    > }
    >
    > $pop->quit;
    > my $b64str;
    > open(UD, ">nu.jpg") or die "$!";
    >
    > open(IND, "<mail2.txt") or die "$!";
    > my $part2decode = 0;
    > my $line = 0;
    >
    > while(<IND>) {
    > if ( /Content-Disposition/i ) {
    > $part2decode++;
    > }
    > if ( $part2decode ) {
    > $line++;
    > }
    > if ( /Boundary/ ) {
    > $part2decode = 0;
    > }
    > if ( $part2decode && $line > 1 ) {
    > my $encoded = $_;
    >
    > my $decoded = decode_base64($encoded);
    > print UD $decoded;
    > }
    > }
    > close(UD);
    > close(IND);
    >
    >
    >
    >
    >
    >
    Bjorn Jensen, Mar 22, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Wallace
    Replies:
    0
    Views:
    1,049
    James Wallace
    Oct 17, 2003
  2. Anatoli Trifonov

    ASP Control names in resulting HTML

    Anatoli Trifonov, Jun 6, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    343
    John Saunders
    Jun 7, 2004
  3. Dil via .NET 247

    SHA1 length of resulting hash

    Dil via .NET 247, Aug 4, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    17,941
    Dil via .NET 247
    Aug 4, 2004
  4. VB Programmer

    How get resulting HTML of datagrid

    VB Programmer, Aug 4, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,540
    VB Programmer
    Aug 4, 2004
  5. VB Programmer

    How send email of resulting aspx page?

    VB Programmer, Apr 17, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    766
    VB Programmer
    Apr 19, 2005
Loading...

Share This Page