Re: accessing mail server

Bill Mills-Curran (bcurran@clariion.com)
Fri, 5 Nov 1999 07:45:55 -0500 (EST)


On Fri, 5 Nov 1999, dynacs wrote:

> Date: Fri, 5 Nov 1999 09:10:08 +0530
> From: dynacs <dynacs@blr.vsnl.net.in>
> To: libwww@perl.org
> Subject: accessing mail server
> 
> I am trying to write a perl program that would periodically access a 
> mail server and download the mails and put them in a particular directory on my local machine.
> I am using ActiveState perl 5.09 on NT 4.0 server.
> Please suggest some alternatives.
> Thanks
> Rajesh
> 
> 

You need 2 things:

1.  You have to have a mail service to allow the download -- usually
    IMAP or POP3.  These are commonly available utilities.

2.  Run fetchmail (again commonly available) on the client side to
    download the mail to your local machine.

    When you set up fetchmail, you'll need to use your own "MDA" a
    mail delivery agent.  I wrote one in perl.  Here it is (you'll
    have to modify the magic cookie and directory names):

#!/usr/local/tls/bin/perl

use strict;
use POSIX;
use IO::File;
use Fcntl;

my ($folder) = $ENV{'HOME'} . '/local/Mail/pop/popInbox';

# flock
my $folderHandle = IO::File->new($folder, O_RDWR|O_APPEND|O_CREAT);
die unless (defined($folderHandle));
flock($folderHandle, &Fcntl::LOCK_EX);

# lock file
my $lockHandle = IO::File->new("$folder.lock",
			    O_WRONLY|O_CREAT|O_EXCL);
die unless (defined($lockHandle));

print($folderHandle "\nFrom bcurran\@nowhere ",
      strftime("%a %b %d %H:%M:%S %Z %Y\n", localtime()));

my $line;
while (defined($line=<STDIN>)) {
    $line =~ s/^From />From /;
    print($folderHandle $line);
}

close($lockHandle); unlink("$folder.lock");
close($folderHandle);


This is how I get all my email from two different email servers -- one
a UNIX sendmail/POP3 server and the other a MSExchange/IMAP server.

Bill