user4979733
user4979733

Reputation: 3411

ldap3: Get email address of user

I am using the python ldap3 module. I am able to connect to my company's LDAP service. I would like to query with a username and get the first/last name + email address of this user.

I say:

results = conn.search('DC=corp,DC=XXXXXX,DC=com',
                      "(&(objectClass=person)(sAMAccountName=" + user_id + "))")

When I run this, I get a match:

[DN: CN=YYYY\, ZZZZ,OU=Workers,DC=amr,DC=corp,DC=XXXXXX,DC=com - STATUS: Read - READ TIME: 2020-01-14T20:43:16.146874]

So YYYY is the last name, ZZZZ is the first name. But the email is not present. How do I retrieve that? Thanks.

Upvotes: 3

Views: 10374

Answers (1)

user4979733
user4979733

Reputation: 3411

Need to specify the attributes to return. For example:

results = conn.search('DC=corp,DC=XXXX,DC=com',
                      "(&(objectClass=person)(sAMAccountName=" + user_id + "))",
                      ldap3.SUBTREE,
                      attributes=['mail', 'sn', 'givenName'])

Upvotes: 5

Related Questions