Script to download email and attachment using perl

Download email and attachment using Perl script.

#!/usr/bin/perl

use strict;
use warnings;

use Net::POP3;
use Email::MIME;
#use Email::MIME::Attachment::Stripper;

my $pop3server = ‘mypopservername’;
my $username   = ‘myemail@address’;
my $password   = ‘myPassW0rd’;

my $popclient = Net::POP3->new( $pop3server, Timeout => 60 );

if ( $popclient->login( $username, $password ) > 0 )
{
foreach my $msgnum ( keys %{$popclient->list() } )
{
my $msg     = $popclient->get($msgnum);
my $message = join ”, @{$msg};

my $parsed = Email::MIME->new($message);
print ” No new mail arrived .\n”;

foreach my $part ( $parsed->parts() )
{
my $filename = $part->filename(1);

print “Read messagae $part .. my $filename. \n”;
open FILE, ‘>’, $filename
or do { warn “can’t open $filename – $!”; next; };
print FILE $part->body();
close FILE;
}
#$popclient->delete($msgnum); # remove # if you do not want to keep the message in the mailbox
}
}
$popclient->quit();

 


You would require to download the Net::POP3 and Email::MIME perl module for this script.

You can either download install from cpan or http://search.cpan.org individual installation.

This entry was posted in Perl Script and tagged , , , , , . Bookmark the permalink.

Leave a Reply