Distribution List Email Addresses

This script will display all user accounts in a selected Distribution List, including other groups. It’s a simple script but very handy!

The script will display the following fields

member or member;range
mail

Like all the scripts I’m putting here I’ve left them in pretty much unedited so you will have the same formatting as I do when viewing them. My apache server is also setup with the mod_auth_sspi.so so only AD users can access them and you’ll see this reflected in the logging I have also on all my scripts.

Code Snip

function GetInfo($val)
{
	global $type;
	$data = "";
	$GN = GetGroupName($val);
	$GA = GetUserAddress($val);

	if($type==1) // another group
	{
		$data = " <td class=a16a><a href=\"dlmemberaddresses.php?group=".$GN."\">".$GN."</a></td>\n";
		$data .= " <td class=a16a>".$GA."</td>\n";
	}
	else // user
	{
		$data = " <td class=a16a>".$GN."</td>\n";
		$data .= " <td class=a16a>".$GA."</td>\n";
	}

	return $data;
}

function GetUserAddress($val)
{
	global $type;

	$ds2 = ldap_connect("server-dc.global.mydomain.com");
	$OU2 = "OU=My Company, DC=global, DC=mydomain, DC=com";
	$val3 = "";
	$info2 = "";

	$val2 = substr($val, 0, (strpos($val, ",")));
	$OU2 = substr($val, (strpos($val, ",")+1),strlen($val));

	$r2 = ldap_bind($ds2, "mydomain\eldap", "password");
	$sr2 = @ldap_search($ds2, $OU2, "(&amp;(objectClass=person) (".$val2."))");
	$info2 = @ldap_get_entries($ds2, $sr2);

	if($info2["count"]==0) // not a user or user doesnt exists? try looking for a group
	{
		$r2 = ldap_bind($ds2, "mydomain\eldap", "password");
		$sr2 = @ldap_search($ds2, $OU2, "(&amp;(objectClass=group) (".$val2."))");
		$info2 = @ldap_get_entries($ds2, $sr2);
	} 

	for($i=0; $i<$info2["count"]; $i++) // loop through groups found, should only ever be 1
	{
		@$val3 = $info2[$i]["mail"][0];

		if(in_array("person", $info2[$i]["objectclass"]))
			$type = 0;
		else
			$type = 1;
	}

	ldap_close($ds2);

	return $val3;
}

http://www.arden.ie/wp-content/plugins/downloads-manager/img/icons/winrar.gif Download: Distribution List Email Addresses (4.44KB)
Added: 22/11/2009
Description: PHP script to list all email addresses in it.

Leave a Reply