Satish@Stag has asked for the wisdom of the Perl Monks concerning the following question:

I am able to retrieve the attachments from the POP3 server. But before retrieving I would like to compare the size of the attachment with the available disk space which I am not able to do. Thanks in advance.
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-s +tream)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 down +loading file "null" of 0KB } else { open my $fh, ">", "$attachment_dir/$fi +lename" or croak $!; binmode $fh; print $fh $_->body; $pop->delete($i); } } } $pop->quit;

Replies are listed 'Best First'.
Re: How to find the size of an attachment in a POP3 server
by rhesa (Vicar) on Sep 05, 2007 at 11:34 UTC
    I don't think you can get the attachment size directly from the POP server. The MIME headers usually do not contain a "Content-Length" header. The best you can do is use a heuristic, by looking at the total message size, which you can get like this:
    my $size = $pop->list( $msgnum );
    In general, that number will be higher than the attachment size, because it's the raw byte size of the complete message: it contains the message headers, any text body, the MIME headers for each attachment, and it's inflated due to the attachment(s) being encoded. You might want to account for that by taking a percentage off. In any case, you can be reasonably certain the attachment(s) will be smaller than $size, which I suppose will work for your purpose.
      I agree on the mechanism. A few more thoughts:

      You can retrieve the first $N lines from a mail message from the POP3 server, with top. See Kill that Swen worm on your POP3 account! for example code.

      You can probably read past the text part of the message and into the first lines from the attachments. You can then check what kind of encoding is used (typically it'll be base64) and what's the line length — that typically is fixed for an attachment, or even for all attachements. From this, you can deduce the exact ratio binay size/transmission size. If you don't care about that high precision, for base64, the estimated ratio 3/4 will be very close.

      And from this, you can calculate a very good estimate of the size of a decoded attachment.

      Thanks a lot...its working