He (صلى الله علیه وسلم) used to say:
All actions are by intention, and every man shall have what he intended. 
 Nawawi says in Rawdah at-Taalibeen (1/224 published by Maktab al-Islami):
"The intention is the purpose, so the person about to pray brings to mind that prayer and what is relevant of its characteristics, such as which prayer it is, whether it is obligatory etc. & he brings these things together in his intention with the first takbeer."
 Bukhaari, Muslim and others. It is given in Irwaa' (no. 22).