Phone will need to be unlocked in order to use other networks. As you've indicated it was previously with O2, your mate will need to call them and get them to unlock it.
Once that whole process is done, as soon as you put in the TMobile sim and restart the phone, it will setup the right correct...