Is it possible to control a User's Role based on their Email Address?
"Auto Assign Role" and "Apply for role" modules seem to be close to what you are asking for.

https://www.drupal.org/project/autoassignrole
https://www.drupal.org/project/apply_for_role

If they don't fit your exact use case then I suggest looking at the way autoassignrole_user_presave() works as a starting point for custom coding. http://cgit.drupalcode.org/autoassignrole/tree/autoassignrole.module#n183