Perl based POP/IMAP downloader was RE: POP3 download from ISP

  • From: "William Holmes" <wtholmes@xxxxxxxxxxxxxx>
  • To: "[ExchangeList]" <exchangelist@xxxxxxxxxxxxx>
  • Date: Tue, 8 Apr 2003 11:20:22 -0400

#=====================================================================
# Internet mail downloader ....
#
#    This script will download mail from internet e-mail accounts and
#    re-send it via smtp to an internal smtp server such as send-mail
#    or Microsoft Exchange. It can handle both IMAP and POP3 Accounts.
#    when used with IMAP a specific folder is defined for downloading.
#
#    A file called accounts.txt contains the account information for
#    the mail accounts to be downloaded. No attempt has been made to
#    secure this file. As this file has to contain passwords you should
#    take precautions to secure it by some other means. At some point
#    I should add the code to allow for the config file (or at least the
#    passwords to be encrypted. Of course this would require reversable
#    encryption which isn't all that secure anyway.
#
#    William Holmes
#    Deparment of Computer Science
#    Cornell University
#    Ithaca, NY
#    wholmes@xxxxxxxxxxxxxx
#
#
# Accounts.txt Format.
#
# Each account needs a line in the Accounts.txt file in the following
format.
#
#
mailserver|accounttype|username|password|destinationaccount|smptserver|I
MAPfolder
#
#    where:    mailserver = your internet mail server
#              accounttype = POP -or- IMAP
#              username = POP or IMAP account username
#              password = your IMAP or POP account password
#              destinationaccount = the smtp destination ie:
you@yourserver
#              smtpserver = your smtp server yourserver.youdomain
#              IMAPFolder = the IMAP folder you want to download from
(not needed with POP)
#
#    For example:
pop-mail.earthlink.net|POP|yourname|yourpassword|you@localserv|localsrv.
com
#
#    -or for IMAP -
#
#
pop-mail.earthlink.net|POP|yourname|yourpassword|you@localserv|localsrv.
com|downloadedmail
#
#
#
#    You can create accounts as you want one per line in this file ....
#
#
# A couple more important notes:

# This script deletes the downloaded messages from the server when it
through downloading them.
# There is not support for leaving messages on the pop server or in the
selected IMAP server.
# In order to add this some local mechanism for tracking which messages
have already been
# downloaded would need to be added.
#
# Mail is downloaded to a local directory and queued. It is then sent
from the local directory
# to the specified SMTP server. There is a QUEUE file the mananges this.
#
# To run this script you need perl. If you don't have perl or know what
perl is check out
# www.activestate.com. You can dowonload perl for free from there. You
will also need to
# add serveral perl packages to your perl installation. You need all of
the modules that are
# shown in the use statments below.
#
# Finally. This script works great for me. It may not work for you at
all. If it helps you out
# Great.
#
#-----------------------------------------------------------------------




use Mail::IMAPClient ;
use Mail::POP3Client ;
use Mail::Header;
use Mail::Address ;
use Net::SMTP;
use IO::File ;
use Fcntl qw(:DEFAULT :flock);
use Sys::Hostname;



our $DLServer ;
our $User ;
our $Password ;
our $SendTo ;
our $SMTPServer ;
our $IMAPFolder ;
our $From ;

our $Message ;

sub UniqueFileName {
    
#
# Generate a unique filename ....
#
    srand( time() ^ ($$ + ($$ << 15))) ;

    while(1) {
        $char_index=0 ;
        for($num = 65 ; $num < 91 ; $num++){$passchar[$char_index++] =
chr($num);} 
        $Filename="" ;
        $TIME = time() ;
        @FOO = ($TIME=~m/(\d)/g) ;
        $Filename = "" ;
        foreach $num(@FOO){
        $Filename= $Filename . $passchar[$num] ;
        }
        $Filename = $Filename . "." ;
        for($num = 0; $num < 12; $num++){
                $Filename .= $passchar[rand($char_index)];
        }
        $Filename = $Filename . ".EML" ;
        return $Filename unless (-e $Filename) ;
    }
}


#
# Send Mail Via SMTP ...
#

sub SendMail{
    
    sysopen(QUEUEFILE, "Queue", O_RDWR | O_CREAT) or die "can't open
dababase file: $!";
    flock(QUEUEFILE, LOCK_EX) or die "can't write-lock counterfile: $!";
    seek(QUEUEFILE, 0, 0) or die "Unable to rewind" ;
    @QueueEntries = <QUEUEFILE> ;
    foreach $Entry (@QueueEntries){
        chomp ($Entry) ;
        ($Filename, $From, $SendTo) = split(/\|/ , $Entry) ;
    
        open(MESSAGE, $Filename) or next ;
        @MSG= <MESSAGE> ;
        close MESSAGE ;
        my $length=@MSG ;
        my $i ;
        for ($i=0; $i < $length ; $i++){$MSG[$i] =~ s/\r\n/\n/g ;}
        $Message=join("",@MSG) ;


    $hostname = hostname();
    my $smtp = Net::SMTP->new($SMTPServer,
                           Hello => $hostname,
                           Timeout => 120,
                           Debug   => 0,
                          );
    
        print "Banner " . $smtp->banner() . "\n" ;
        print "Mail Domain " . $smtp->domain() . "\n" ;
        print "[Sending message via SMTP]: From: <$From> To: <$SendTo>
\n" ;
        $smtp->mail($From);
        $smtp->recipient($SendTo) ;
        $smtp->data() ;
        $smtp->datasend($Message);
        $smtp->dataend();
        $smtp->quit;
        undef $smtp ;
        unlink ($Filename) ;
    }
    seek(QUEUEFILE, 0, 0) or die "Unable to rewind queuefile" ;
    truncate(QUEUEFILE, tell(QUEUEFILE)) or die "can't truncate Queue";
    close(QUEUEFILE) or die "Can't Close The Queue";
}

#
# Get POP Mail ...
#

sub GetPOP3Mail() {
    
    my $pop = new Mail::POP3Client( USER => $User,
                                    PASSWORD => $Password,
                                    HOST => $DLServer,
                                    DEBUG => 0) ;
    
    my $POPMessages =  $pop->Count() ;
    print "There are $POPMessages messages to process. \n" ;
    sysopen(QUEUEFILE, "Queue", O_RDWR | O_CREAT) or die "can't open
dababase file: $!";
    flock(QUEUEFILE, LOCK_EX) or die "can't write-lock counterfile: $!";
    seek(QUEUEFILE, 0, 2) or die "Seek end of file failed for Queue
file.";
    
    my $msgcount = 0 ;
    my $ID ;
    for ($ID=1 ; $ID<=$POPMessages; $ID++){
        $msgcount++ ;
        @Header=$pop->Head($ID) ;
        my $MailHead = new Mail::Header;
        my %HdrHash ;
        $MailHead->unfold()->header(\@Header) ;
        my $HdrRef = $MailHead->header_hashref();
        foreach my $key(keys %$HdrRef){
            my $ValRef = $$HdrRef{$key} ;
            $HdrHash{$key} =  join ("" ,@$ValRef) ;
            chomp($HdrHash{$key}) ;
        }                               
        # Get the senders email address ...
        if (!($HdrHash{'From'})){
            $pop->Delete($ID) ;
            next ;
        } 
        my @addrs=Mail::Address->parse($HdrHash{'From'}) ;
        $From = $addrs[0]->address() ;
        print "[Retrieving POP Message]: $ID.  Received From: <$From>
\n" ;
        my $Filename = UniqueFileName() ;
        my $msgfile = IO::File->new(">$Filename") ;
        $pop->HeadAndBodyToFile($msgfile, $ID) ;
        printf QUEUEFILE ("%s|%s|%s\n", $Filename, $From,  $SendTo) ;
        $msgfile->close() ;
        $pop->Delete($ID) ;
        if($msgcount >= 50){last ;}
    }
    truncate(QUEUEFILE, tell(QUEUEFILE)) or die "can't truncate Queue";
    close(QUEUEFILE) or die "Can't Close The Queue";   
    $pop->Close() ;
    
}
    
#
# Get IMAP Mail ....
#

    
sub GetImapMail(){
    
    my $imap = Mail::IMAPClient->new( Server =>$DLServer, 
                                                    User   => $User,
                                                    Password =>
$Password
    );
    if ($imap){
        
    # Set IMAP options ...
    
        $imap->Debug(0);
        $imap->Strip_cr() ;
        $imap->peek(0) ;
        $imap->Uid(1) ;

        $imap->select($IMAPFolder) ;
        $ptr  = $imap->search("ALL") ;
        @MessageIDs = @$ptr ;
        $num=@MessageIDs ;
        print "There are $num messages to process.\n" ;
        sysopen(QUEUEFILE, "Queue", O_RDWR | O_CREAT) or die "can't open
dababase file: $!";
        flock(QUEUEFILE, LOCK_EX) or die "can't write-lock counterfile:
$!";
        seek(QUEUEFILE, 0, 2) or die "Seek end of file failed for Queue
file.";    
        my $msgcount = 0 ;
        foreach $ID(@MessageIDs){
            $msgcount++ ;
            for $h (values %{$imap ->parse_headers($ID,"From")}){
                foreach $part (@$h){
                    my @addrs=Mail::Address->parse($part) ;
                        $From  = $addrs[0]->address() ;
                    print "[Retrieving IMAP Message]: $ID Received From:
<$From>\n" ;
                }
            }
            my $Filename = UniqueFileName() ;
            if($imap->message_to_file("$Filename" , $ID)){
                printf QUEUEFILE ("%s|%s|%s\n", $Filename, $From,
$SendTo) ;
            }
            $imap->delete_message($ID) ;
            if($msgcount >= 500){last ;}
        }
        truncate(QUEUEFILE, tell(QUEUEFILE)) or die "can't truncate
Queue";
        close(QUEUEFILE) or die "Can't Close The Queue";    
        $imap->expunge() ;
        $imap->disconnect() ;
    }else{
        print "Unable to Connect.\n" ;
    }
}

#
# Main ...
#

my $Type ;

while(1) {
    open(ACCOUNTS, "Accounts.txt");
         my @Accounts=<ACCOUNTS> ;
    close(ACCOUNTS) ;
    foreach $Entry(@Accounts){
        chomp($Entry) ;
        ($DLServer, $Type,  $User, $Password, $SendTo, $SMTPServer,
$IMAPFolder) = split(/\|/, $Entry) ;
        print "Processing Account [$Type/$User\@$DLServer] \n" ;
        if($Type eq "IMAP"){
            if(!($IMAPFolder)){$IMAPFolder="INBOX" ;}
            GetImapMail ;
        }elsif($Type eq "POP"){
            GetPOP3Mail ;
        }
    }
    SendMail() ;
#
# Wait 10 minutes and then start all over again ...
#
    sleep (600) ;
}



-----Original Message-----
From: Paul Taylor [mailto:paul@xxxxxxxxxxxxxxx] 
Sent: Tuesday, April 08, 2003 7:06 AM
To: [ExchangeList]
Subject: [exchangelist] RE: POP3 download from ISP

http://www.MSExchange.org/

Need pop3 connector 

third party software

ie msmailessentails etc.

 

 

 

-----Original Message-----
From: Achmad Mursalin [mailto:ach_m@xxxxxxxxx] 
Sent: Tuesday, April 08, 2003 2:19 AM
To: [ExchangeList]
Subject: [exchangelist] POP3 download from ISP

 

http://www.MSExchange.org/ 

how to configure POP3 download form Mailserver ISP:

I have exchange server 2000, I have creat POP3 logon in mail server ISP:

How to configure in my Exchange 2000 so that I send and retrive mail
from mailserver ISP.I do retrive mail with software mdaemon.

Thanks

 

  _____  

Do you Yahoo!?
Yahoo! Tax Center
<http://us.rd.yahoo.com/finance/mailsig/*http:/tax.yahoo.com>  - File
online, calculators, forms, and more Free Anti-Spam Download: Try
Multi-layered, Accurate Spam Filtering with SurfControl E-mail Filter
http://www.surfcontrol.com/go/zmsexdl1
------------------------------------------------------ List Archives:
http://www.webelists.com/cgi/lyris.pl?enter=exchangelist Exchange
Newsletters: http://www.msexchange.org/pages/newsletter.asp Exchange
FAQ: http://www.msexchange.org/pages/larticle.asp?type=FAQ
------------------------------------------------------ ISA Server
Resource Site: http://www.isaserver.org Windows Security Resource Site:
http://www.windowsecurity.com/ Windows 2000/NT Fax Solutions:
http://www.ntfaxfaq.com
------------------------------------------------------ You are currently
subscribed to this MSExchange.org Discussion List as:
paul@xxxxxxxxxxxxxxx To unsubscribe send a blank email to
$subst('Email.Unsub')


Free Anti-Spam Download: Try Multi-layered, Accurate Spam Filtering with
SurfControl E-mail Filter
http://www.surfcontrol.com/go/zmsexdl1

------------------------------------------------------
List Archives: http://www.webelists.com/cgi/lyris.pl?enter=exchangelist
Exchange Newsletters: http://www.msexchange.org/pages/newsletter.asp
Exchange FAQ: http://www.msexchange.org/pages/larticle.asp?type=FAQ
------------------------------------------------------
ISA Server Resource Site: http://www.isaserver.org
Windows Security Resource Site: http://www.windowsecurity.com/
Windows 2000/NT Fax Solutions: http://www.ntfaxfaq.com
------------------------------------------------------
You are currently subscribed to this MSExchange.org Discussion List as:
wtholmes@xxxxxxxxxxxxxx
To unsubscribe send a blank email to
$subst('Email.Unsub')


Other related posts:

  • » Perl based POP/IMAP downloader was RE: POP3 download from ISP