Can you teleport whilst under attack?

Yes. But if it's crucial that you have to live, make sure it's a one click teleport (teleport tablets, ectophial, etc.). It's also a good idea to bring a ring of life with you if you're risking a lot. It doesn't work after 30 wilderness though. You can use teleports such as a glory or explorer's ring 3 but those prompt could take longer to use.