Extracting e-mail addresses from Active Directory

The script below can be used to create a text file containing e-mail addresses from Active directory. You can then pick up that file on your mail gateway and use it as a source for valid recipients (via http protocol for example). Sure, you can also contact AD directly every time you need a recipient check. However, this may be a resource intensive task for larger environments and firewall arrangements for AD access can be a security issue.

# Perl script which extracts e-mail addresses from Active Directory
# requires dsquery
use strict;
my $adrfile = "extadr.txt"; # destination file, customize!!
open ADDRLIST, "dsquery * domainroot -limit 0 -attr name proxyAddresses |" or die $!;
open ADDROUT, "> $adrfile" or die $!;
while (<ADDRLIST>)
 my $line = lc $_;
 # exclude patterns, customize!!
 next if $line =~ /noreply/; # addresses starting with noreply
 my @adrlist = ($line =~ /smtp:(+)\;/g);
 next if not @adrlist;
 foreach my $adr (@adrlist)
  print ADDROUT "$adr\n";
close ADDROUT;

Release news