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

Hi All,

Need: Process a new email

OS : Windows 2000

email server: Microsoft Exchange Server

Email client : Outlook

I want to parse an email whenever a new email arrives. How can I do this for the above configuration.

Alos do I have to change some settings on the Exchange server ( alternatively is there some necessary setting needed on the exchange server side).

Thanks for your time.

  • Comment on How to find when a new email arrives in my mailbox (outlook)

Replies are listed 'Best First'.
Re: How to find when a new email arrives in my mailbox (outlook)
by vladdrak (Monk) on Sep 17, 2004 at 00:28 UTC
    This works for me, season to taste:
    use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; my $username="username"; # fix me my $outlook = Win32::OLE->CreateObject("Outlook.Application"); OLEErrChk("Create Outlook Object"); my $namespace=$outlook->GetNameSpace("MAPI"); OLEErrChk("Get MAPI Namespace"); my $ol=Win32::OLE::Const->Load($outlook); OLEErrChk("Loading Outlook Constants"); my $logon=$namespace->Logon($username); OLEErrChk("Logging in"); my $folder=$namespace->Folders(4); OLEErrChk("Opening Folders"); my $inbox=$folder->Folders("Inbox"); OLEErrChk("Get a handle to the Inbox"); my $i=1; while (my $msg=$inbox->Items($i)) { OLEErrChk("Got message id $i"); my @lines=split/\n/,$msg->Body; my $skills=0; my %data=(); LINE: for my $line (@lines) { # do something with the message body.. } my $subject=$msg->Subject; if ($subject =~ /something/) { # blah } $i++; } sub OLEErrChk { my $oleaction=shift; my $errNum=Win32::OLE->LastError; if ($errNum != 0) { die "$oleaction failed: $errNum\n"; } }
      Can you say a few words about how this is working? I can understand the Perl well enough, but don't know enough about Windows OLE.

      I see there is a place to configure the userid, but no place to configure the server name or password. So I'm guessing this is getting the information by doing all the interaction through Outlook? Cool.

      Also when you do this:

      Win32::OLE->CreateObject("Outlook.Application");

      and then later load the object, are you starting Outlook at that point? Will this still work if Outlook is already started?

      Thanks for posting the code btw.

        I have, I hesitate somewhat to admit, written some similar code myself (hesitation due to general dislike of MSFT APIs), so I will try to answer, though I don't have a Windows box here to verify the code on.

        If Outlook is already running, this code will work without any username or poassword prompts. If not, running the program will cause Outlook to automagically start and present the username/password prompt in its ordinary way.

        The line you quote creates a new instance of an object that one uses, through OLE, to interact with Outlook programmatically. It will indeed start Outlook for you if it is not already running.

        The rest of the code is just the usual code for opening a mailbox in Outlook. The strange method and argument names are simply a reflection of the way MAPI data sources are structured. You can find a VB equivalent of that code fairly easily by searching around the 'net, though Perl is a less common choice for this sort of thing (but I presume it works fine).

Re: How to find when a new email arrives in my mailbox (outlook)
by ikegami (Patriarch) on Sep 16, 2004 at 23:50 UTC

    I have a vague recollection of hearing that Exchange supports POP and/or IMAP. Have you tried Net::POP3? (I believe it even comes with perl.)

Re: How to find when a new email arrives in my mailbox (outlook)
by InfiniteSilence (Curate) on Sep 17, 2004 at 00:22 UTC
    You may think this is a crazy solution, but you can (provided you have access to the Exchange Server Manager) reroute all messages to an smtp server. There IS a perl based SMTP server -- details for which available at packages.debian.org. Some info. for reconfiguring Exchange to route the message is here

    Celebrate Intellectual Diversity