0

I have the following bit of code which works for some records in active director but returns "Notice: Undefined index: mail in C:\xampp\htdocs\utils.php on line 57".

Line 57 is "$email = ($data[$i]["mail"][0]);"

Could this be a problem with the code or is there something in AD that is preventing the access of the "mail" property. It only returns the error on some records not all. For most records, I get the correct return back.

Thanks in advance.

$attributes = array("samaccountname", "sn", "givenname", "mail");

$search_filter = "(&(givenname=$firstName)(sn=$lastName))";

$search = ldap_search($ds, $dn, $search_filter, $attributes, 0, 0);

$data = ldap_get_entries($ds, $search);

$rows = array();
for ($i=0; $i<$data["count"]; $i++) {
    if (isset($data[$i]["sn"][0], $data[$i]["givenname"][0])) {
        $email = ($data[$i]["mail"][0]);
        echo $email;
        array_push($rows,$email);
    }

}

echo json_encode($rows);
  • can you do `print_r($data)` and post the result? – Long Kim Jan 17 '18 at 02:00
  • Thanks Long_Kim. Using your suggestion, I managed to work out the some users had two accounts on AD and the admin account did not have an email address. The array was returning multiple records and therefore returning an error for the record with no "mail" attribute. – Rovlin Moodley Jan 17 '18 at 13:30
  • no worries, glad it helped. – Long Kim Jan 17 '18 at 22:27

0 Answers0