You are using Moodle 3.3 and the code in question is located here:
https://github.com/moodle/moodle/blob/MOODLE_33_STABLE/lib/moodlelib.php#L5924
5922 if ($user->id > 0) { 5923 $context['touserid'] = $user->id; 5924 $context['tousername'] = $user->username; // <-- Line in question. 5925 }
This is located in function email_to_user. This essentially means that user object does not have username value. Why? You will have to figure that one.