To find size of an attachment in a POP3 server

Discussion in 'Perl Misc' started by satyakm79@gmail.com, Sep 5, 2007.

  1. Guest

    Hi all,

    I am trying to retrieve all the attachments from a POP3 server. But
    before retrieving I would like to get the size of the attachment and
    compare it with the available disk space in my local station and then
    retrieve. Can anyone suggest me how to find the size of an attachment?
    I am able to find the available disk space using File::Find. Plz find
    my script below :

    #!/usr/bin/perl

    use Carp;
    use Email::MIME;
    use File::Basename;
    use Net::pOP3;
    use File::Find;

    my $server = '192.168.100.254';
    my $receiveruname = 'admin';
    my $password = 'admin';
    my $attachment_dir = 'D:\\Attachments\\';

    my $pop = Net::pOP3->new($server);
    croak "Couldn't connect to the server.\n\n" unless $pop;
    my $num_messages = $pop->login( $receiveruname, $password );
    croak "Connection trouble network password user ..."
    unless defined $num_messages;

    for my $i ( 1 .. $num_messages ) {

    my $aref = $pop->get($i);
    my $em = Email::MIME->new( join '', @$aref );

    for ( my @parts = $em->parts ) {
    print $_->content_type, "\n";
    next unless $_->content_type =~ m(^application/octet-stream)i;
    my $filename = basename( $_->filename || '' );
    my $basefilename = $filename || 'UNNAMED';

    my $filesize = -s "$filename" ;
    print "\nFilesize of $filename = $filesize
    \n";

    if ( $filename eq "null" ) {
    $pop->delete($i); # To avoid
    downloading file "null" of 0KB
    }
    else {
    open my $fh, ">", "$attachment_dir/
    $filename" or croak $!;
    binmode $fh;
    print $fh $_->body;
    $pop->delete($i);
    }

    }
    }

    $pop->quit;
    , Sep 5, 2007
    #1
    1. Advertising

  2. wrote:
    >
    > I am trying to retrieve all the attachments from a POP3 server. But
    > before retrieving I would like to get the size of the attachment and
    > compare it with the available disk space in my local station and then
    > retrieve.


    It can't be done. You can get the size of the email message before retrieving it:

    perldoc Net::pOP3
    [ snip ]
    list ( [ MSGNUM ] )
    If called with an argument the "list" returns the size of the message
    in octets.

    If called without arguments a reference to a hash is returned. The
    keys will be the "MSGNUM"’s of all undeleted messages and the values
    will be their size in octets.


    But attachments can be encoded/compressed/encrypted which can change the size
    of the original file.

    > Can anyone suggest me how to find the size of an attachment?


    Download it, unencode it, uncompress it and then you will have its size.

    Or there may be headers with the attachment that describe the file size so you
    have to at least read enough of the message to get all of the headers (if you
    trust that the file sizes in the headers are correct.) [Think DOS attack.]



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Sep 5, 2007
    #2
    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. pk
    Replies:
    3
    Views:
    789
    Roedy Green
    Feb 13, 2006
  2. p-nut

    Pop3 autosave attachment.

    p-nut, May 26, 2004, in forum: Python
    Replies:
    1
    Views:
    469
    fishboy
    Jun 4, 2004
  3. Sick Monkey

    POP3 - Saving an image attachment only.

    Sick Monkey, Aug 24, 2007, in forum: Python
    Replies:
    0
    Views:
    361
    Sick Monkey
    Aug 24, 2007
  4. Deepa Rajendran
    Replies:
    0
    Views:
    126
    Deepa Rajendran
    Feb 23, 2009
  5. Ted Byers
    Replies:
    0
    Views:
    245
    Ted Byers
    Mar 20, 2009
Loading...

Share This Page