Solved

PERL MS Exchange address book (web based)

Posted on 2006-07-11
5
464 Views
Last Modified: 2008-01-09
Hi everyone,

I am building an online application that would access the MS Exchange address book and display company contacts, aside many other things that this app does. I was wondering if anyone has done that in PERL and if PERL holds any libraries that help do that. If you can provide a code example it would be just amazing. I looked at the CPAN but I could not find anything there.

Thanks for you help.

L.
0
Comment
Question by:ledeni
  • 3
5 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 17086372
You could try using LDAP:  http://search.cpan.org/~gbarr/perl-ldap-0.33/lib/Net/LDAP.pod
MS Exchange 5.0 is compatible with LDAP
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 300 total points
ID: 17086915
Here's an example pulled from one of the scripts I use here at work to connect to our Exchange 5.5 box.  It uses ADO (basically an LDAP example but hidden underneath ADSI).  I went with it instead of straight LDAP because I had issues with the login.  If you have anonymous LDAP access enabled on your exchange server, normal LDAP shouldn't be a problem.  And the login problems may have just been me anyway.  If you're trying to query it from a non-windows box, this script won't work and normal LDAP will probably be your only option.

No matter which way you go a reference for the LDAP schema for Exchange 5.5 is here: http://www.microsoft.com/technet/prodtechnol/exchange/55/support/adsi.mspx

Ignore the commented section, I pull the SID account and do a SID to account name translation to get the windows account name for the mailbox-- chopped that section out because it doesn't pertain to your question.

Anyway, the following works for me:

use Win32::OLE;

my $conn = Win32::OLE->new('ADODB.Connection');

my $query = '<GC://YOUREXCHANGESERVERNAME:389>;(objectClass=organizationalPerson);Assoc-NT-Account,Uid,ADsPath,cn,givenName,sn,mail,initials,telephoneNumber;subtree';

if ($conn) {

   $conn->{Provider} = "ADsDSOObject";
   $conn->Properties->{"User ID"} = "domainname\\username";
   $conn->Properties->{"Password"} = "userpassword";
   $conn->Properties->{"Encrypt Password"} = 1;

   $conn->Open("ADs Provider");
   my $rs = Win32::OLE->new('ADODB.Recordset');
   $rs->Open($query, $conn);
   if (! $rs->{EOF} ) {

     print "First Name^MI^Last Name^Alias^Emails^Phone #\n";

     my ($email, $nt, $cname, $uid, $fname, $lname, $phone);

     while (! $rs->{EOF} )
     {
      $email = $rs->fields("mail")->{Value};      
      $nt = $rs->fields("Assoc-NT-Account")->{Value};
      $cname = $rs->fields("cn")->{Value};
      $uid = $rs->fields("uid")->{Value};
      $fname = $rs->fields("givenName")->{Value};
      $lname = $rs->fields("sn")->{Value};
      $phone = $rs->fields("telephoneNumber")->{Value};
      $mi = $rs->fields("initials")->{Value};
                #  $sid = $rs->fields("Assoc-NT-Account")->{Value};
      #  $accountname = "";
      #  $accountName = GetNameFromSID($sid) if $sid =~ /^[A-F0-9]+$/ ;
      print "$fname^$mi^$lname^$uid^$email^$phone^$accountName\n" unless $accountName =~ /^zz/i;
      $rs->MoveNext();

     }
     
   }

   $rs->close;
   undef $rs;
   $conn->close;
   undef $conn;
}
0
 

Author Comment

by:ledeni
ID: 17087040
clockwatcher ... thanks a lot ... works like a charm !!

However, I noticed that this is using the win:32 library would this work if the website is deployed on the unix server ??? if not .. do you have any suggestions how it could be done.

Thanks .. credits well spent.

L.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17087191
No.  It won't work on a unix server.  If you're going to do this on unix, you'll need to use straight LDAP.   As I remember it, I had problems with authentication when I tried it that way though.  I'll see if I can put together a few minutes to try and get a working example using Net::LDAP.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17087794
Here's a Net::LDAP example that works for me.  If you don't have SSL enabled for your Exchange's LDAP, it'll transmit the password in clear text.  You might want to try an anonymous bind to see if you have any luck with it (I don't).

use Net::LDAP;

my $exchserver = "MyExchangeServer";
my $ntdomain = "NTDomainName";
my $ntaccount = "NTAccountName";
my $ntpass = "NTPassword";

my $organization = "EXCHANGE_ORGANIZATION";  # the o section of your X.400 address
my $site = "EXCHANGE_SITE";  # the ou section of your X.400 address

my $ldap = Net::LDAP->new("$exchserver");

my $mesg = $ldap->bind( "cn=$ntaccount,cn=$ntdomain", password => $ntpass) ;  #authenticated

# my $mesg = $ldap->bind() ; # anonymous -- this may work for you -- try it first -- doesn't for my set up

$mesg = $ldap->search(
                  base => "cn=Recipients,ou=$site,o=$organization",
                  filter => 'cn=*'
                  );

print "First Name^MI^Last Name^Alias^Emails^Phone #\n";

foreach $entry ($mesg->entries) {

  foreach ( qw(givenName initials sn uid mail telephoneNumber) )
  {
       print $entry->get_value($_)."^";
  }
  print "\n";

}

$mesg = $ldap->unbind;
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 184
Perl Script - Remove row of data based on column value 3 72
Perl 101 11 68
perl to convert excel to csv 3 151
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now