Islam attaches the utmost importance to intentions (niyyah). Umar ibn al-Khattab reported: The Messenger of God said, “Verily, deeds are only with intentions, and every person will have only what they intended. Whoever emigrated to God and His Messenger, his emigration is for God and His Messenger. Whoever emigrated to get something in the world or to marry a woman, his emigration is for that to which he emigrated.” (Sahih al-Bukhari, Hadith No. 54, Sahih Muslim, Hadith No. 1907)
No action is acceptable to God purely on the basis of its outer appearance. He accepts only such actions as are performed with proper intention, and rejects those performed with ill intention. Right intention is the moral purposiveness, which underlies all actions performed solely for God’s pleasure. One who acts on such feelings will be rewarded by God in the Hereafter.
Abdullah, the son of, Imam Ahmad ibn Hanbal said to his father one day, “O father, instruct me.” Ahmad, may God be pleased with him, said, “O my son, have a good intention. You will always be good as long as you intend good.” (Al-Adab al-Shar’iyah by Ibn Muflih 1/104)
Source: Principles of Islam