Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

LDAP address lookup from Mutt

by lachoy (Parson)
on Mar 17, 2001 at 20:45 UTC ( #65152=sourcecode: print w/replies, xml ) Need Help??
Category: E-Mail Programs
Author/Contact Info Chris Winters <>

Mutt allows you to add a hook for an external address lookup program. A common one is LDAP, and this simple script does the lookup for you. Once this is setup, you can enter part of a person's first or last name, hit ^T and the LDAP search will be performed.

Just add the following line to your .muttrc, changing the name of the script to whatever you save it as:

set query_command = "~/bin/ '%s'"

You'll have to set the proper information for the constants HOST and BASE and, if your LDAP server doesn't allow anonymous binds, also pass the full DN mapping to your user record and password to the bind() method. Also, if you have a nonstandard schema you'll have to modify the LDAP property names.


use strict;
use Net::LDAP;

use constant HOST => '';
use constant BASE => 'cn=People, dc=MySite, dc=com';

  print "Searching database... ";
  my $name = shift || die "Usage: $0 filter\n";
  my $ldap = Net::LDAP->new( HOST, onerror => 'die' ) 
                || die "Cannot connect: $@";
  $ldap->bind() or die "Cannot bind: $@";
  my $msg = $ldap->search( base => BASE, 
                           filter => "(|(sn=*$name*)(givenName=*$name*
+))" );
  my @entries = ();
  foreach my $entry ( $msg->entries() ) {
    push @entries, 
        { email      => $entry->get_value( 'mail' ),
          first_name => $entry->get_value( 'givenName' ),
          last_name  => $entry->get_value( 'sn' ) };
  print scalar @entries, " entries found.\n";
  foreach my $entry ( @entries ) {
    print "$entry->{email}\t$entry->{first_name} $entry->{last_name}\n
Replies are listed 'Best First'.
Re: LDAP address lookup from Mutt
by waxhead (Acolyte) on Dec 24, 2002 at 07:40 UTC

    This is my first comment...

    I grabbed this script to give me ldap access to my server, naturally. However my mail attribute is multivalued for a number of entries, so I have made some modifications to the script to handle the multivalued attribute.

    Make sure you change all entries as required.

    #!/usr/bin/perl use strict; use Net::LDAP; use constant HOST => 'ldap.your.domain'; use constant BASE => 'ou=People, dc=your, dc=domain'; use constant VERSION => 3; use constant SCOPE => 'sub'; my $name; my @attributes = qw( dn givenName sn mail ); my $filter = "(|(sn=$name*)(givenName=$name*))"; { print "Searching directory... "; $name = shift || die "Usage: $0 filter\n"; my $ldap = Net::LDAP->new( HOST, onerror => 'die' ) || die "Cannot connect: $@"; $ldap->bind(version => VERSION) or die "Cannot bind: $@"; my $result = $ldap->search( base => BASE, scope => SCOPE, attrs => \@attributes, filter => $filter ); my @entries = $result->entries; $ldap->unbind(); print scalar @entries, " entries found.\n"; foreach my $entry ( @entries ) { my @emailAddr = $entry->get_value('mail'); foreach my $addr (@emailAddr) { print $addr , "\t"; print $entry->get_value('givenName'), " "; print $entry->get_value('sn'), "\n"; } } }

    Make sure you have the Net::LDAP module installed.

    Hope someone finds it of use, and thanks to the original author... I just hacked on what was already provided.

      There's a slight error... sorry about that...

      Move the line creating the filter after the line that shift's the parameter into $name:

      $name = shift || die "Usage: $0 filter\n"; my $filter = "(|(sn=$name*)(givenName=$name*))";

      This works much better.. :-/

        After searching endlessly for a straight forward ldap search script sample that actually returned info I can use immediately and not have to through into an array to further massage(because I don't understand the Net::LDAP interworkings), This is is awesome! This should be the sample script used to teach folks who don't yet understand the Perl OO stuff and would like to use Net::LDAP. Some minor issues with "Odd number of elements in anonymous hash", but otherwise works great and was able to further modify to use. Frankly, the official examples(as someone new to using the Net::LDAP" are not very good as far as the search goes. This example is just fantastic, now I can start digging further for better understand this. But honestly, I don't know why some of these modules are built so complex to use. At any rate, Thanks Lachoy!!
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://65152]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-07-17 04:15 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (354 votes). Check out past polls.