All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
It is better for the person who mentions Allah or recites the Quran to be in a good appearance, and purified from all impurities and dirty things.
Nonetheless, the mention of Allah while there is impurity on the body or garment is not forbidden; but it is not the best situation to mentions Allah or recite the Quran in.
As for the mention of Allah with the heart, then this is permissible in all cases.
‘Aa’ishah narrated that the Prophet used to mention Allah in all his conditions.
As regards mentioning Allah while having sexual intercourse with one’s wife, then it is legislated to say the mention before intercourse and not while having intercourse. Ibn ‘Abbaas narrated that the Prophet said: “If anyone who is about to have sexual intercourse with his wife says “Bismillaah. Allaahumma jannibna Ash-Shaytaan wa jannib Ash-Shaytaan ma razaqtana, (In the name of Allah. O Allah protect us from Satan and protect from Satan (our offspring) whom You will grant us”; then If Allah decrees that they will have a child, Satan will never harm him." [Al-Bukhaari and Muslim]
As regards the mention of Allah during sexual intercourse, it is disliked as it is disliked when relieving oneself.
Allah knows best.