qadwjoh has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Can anyone help me with reading email from an Exchange server? I need to scan my inbox for mail from a certain user and then download any attachements in that email to my machine.

I know there are already a few posts out there on how to do this, but I can't get any of them (ie. those using MAPI or LDAP) to work.

Can you use Net::POP3 or Net::SMTP to do this? I've managed to send email using Net::SMTP with the following...
use Net::SMTP; unless ($smtp = Net::SMTP->new('mailhost')) { print "Fail\n"; } # print domain print $smtp->domain,"\n"; # send test email $smtp->mail($ENV{USER}); $smtp->to('phil@philvickery.com'); $smtp->data(); $smtp->datasend('To: phil@philvickery.com\n'); $smtp->datasend("\n"); $smtp->datasend("A simple test message\n"); $smtp->dataend(); $smtp->quit;
...but can't work out how to read emails.

thanks,
A

Replies are listed 'Best First'.
Re: Reading email on Exchange
by peschkaj (Pilgrim) on Jul 24, 2003 at 18:30 UTC

    You can indeed use the Net::POP3 module to take care of reading email on exchange.

    One thing to keep in mind while reading the email is that the retrieve command (RETR MSG_NUM, $pop->get(MSG_NUM) in perl) will remove the message from the server. In order to get the full text of the message from exchange without removing the message from the server, you will want to send a TOP command with the NUMLINES parameter set to 0.

    Example:

    use Net::POP3; my $pop = Net::POP3->new( "mail.mydomain.com", Timeout => 60 ); $pop->user( "me" ); $pop->pass( "mypass" ); ### or you can use ### $pop->login( "me", "mypass" ); ### or if your server supports it: ### $pop->apop( "me, "mypass" ); $pop->popstat(); # returns number of messages # and total size in bytes $pop->list(); # returns number of messages, # total size in bytes, # and byte size for each message $pop->top( 1, 0 ); # shows the contents # of the first message # in the mailbox ### you might want to run NOOP every once in a while ### if you want to keep the connection alive ### refer to: http://www.faqs.org/rfcs/rfc1939.html ### for a more detailed explanation if needed.

    Hope that helps.

    If you make something idiot-proof, eventually someone will make a better idiot.

    I am that better idiot.

      OK.

      I tried peschkaj's method and got it to work.

      Now any ideas on how to select an email from someone and save the attachement on it?

      thanks,
      A
        Have a look at the Mime::Tools module. That will allow you to parse a multipart email, and save the attachment.
Re: Reading email on Exchange
by blue_cowdawg (Monsignor) on Jul 24, 2003 at 18:27 UTC

          Can you use Net::POP3 or Net::SMTP to do this?

    This depends on your Exchange Administrator's setup of the exchange box. I currently use IMAP to get email from Exchange since I refuse to use the corporate desktop (Winbloze 2000 on top of Novell) and do everything from Linux.


    Peter L. BergholdBrewer of Belgian Ales
    Peter@Berghold.Netwww.berghold.net
    Unix Professional
      Thanks for the help lads.

      Can you post an exmample of how to connect and get email using IMAP?

      A