When any of you enters the mosque, let him send peace upon the Prophet and say: Allāhumma iftahli abwāba rahmatik (O Allah, open for me the gates of Your mercy)
حَدَّثَنَا مُحَمَّدُ بْنُ بَشَّارٍ، حَدَّثَنَا أَبُو بَكْرٍ الْحَنَفِيُّ، حَدَّثَنَا الضَّحَّاكُ بْنُ عُثْمَانَ، حَدَّثَنِي سَعِيدٌ الْمَقْبُرِيُّ، عَنْ أَبِي هُرَيْرَةَ، أَنَّ رَسُولَ اللَّهِ ـ صلى الله عليه وسلم ـ قَالَ
" إِذَا دَخَلَ أَحَدُكُمُ الْمَسْجِدَ فَلْيُسَلِّمْ عَلَى النَّبِيِّ ـ صلى الله عليه وسلم ـ وَلْيَقُلِ اللَّهُمَّ افْتَحْ لِي أَبْوَابَ رَحْمَتِكَ وَإِذَا خَرَجَ فَلْيُسَلِّمْ عَلَى النَّبِيِّ وَلْيَقُلِ اللَّهُمَّ اعْصِمْنِي مِنَ الشَّيْطَانِ الرَّجِيمِ "
Abu Hurayrah (may Allah be pleased with him) reported that the Messenger of Allah (ﷺ) said:
"When any of you enters the mosque, let him send peace upon the Prophet and say: Allāhumma iftahli abwāba rahmatik (O Allah, open for me the gates of Your mercy). And when he leaves, let him send peace upon the Prophet and say: Allāhumma i‘simni min ash-shaytān ar-rajīm (O Allah, protect me from the accursed devil)." In another narration by Al-Hākim: "When he leaves, let him send peace upon the Prophet (ﷺ) and say: Allāhumma ajirni min ash-shaytān ar-rajīm (O Allah, save me from the accursed devil)."
The Prophet (ﷺ) guided the Muslim, when entering the mosque, to send peace and blessings upon the Prophet (ﷺ) by saying: Allāhumma salli wa sallim ‘ala Muhammad (O Allah, send Your blessings and peace upon Muhammad), and then to say: Allāhumma iftahli abwāba rahmatik (O Allah, open for me the gates of Your mercy). And when he leaves, he should send peace upon the Prophet and say: Allāhumma i‘simni min ash-shaytān ar-rajīm (O Allah, protect me from the accursed devil). In another narration by Al-Hākim: And say: Allāhumma ajirni min ash-shaytān ar-rajīm (O Allah, save me from the accursed devil).
Lessons
It is recommended to say this supplication upon entering the mosque and upon leaving it.
This supplication is to be generally recited in all mosques, including the Sacred Mosque.
The specific mention of mercy when entering and seeking protection from the devil when exiting: The one entering is engaged in what brings him closer to Allah and His Paradise, so it is fitting to mention mercy. When exiting, he ventures into the world with its distractions and preoccupations, so it is appropriate to ask Allah for protection and preservation from the devil.
Explanation in Arabic
أرشد النبي صلى الله عليه وسلم المسلم إذا أراد الدخول إلى المسجد أن يسلم على النبي صلى الله عليه وسلم بقوله: اللهم صل وسلم على محمد، ثم ليقل: (اللهم افتح لي أبواب رحمتك).
وإذا خرج فليسلم على النبي وليقل: (اللهم اعصمني من الشيطان الرجيم)، وفي رواية للحاكم: وليقل: (اللهم أجرني من الشيطان الرجيم).
استحباب هذا الدعاء عند دخول المسجد والخروج منه.
الذكر عام في كل المساجد، حتى المسجد الحرام.
تخصيص ذكر الرَّحمة بالدُّخول، والعصمة من الشيطان بالخروج: أنّ الدَّاخل اشتغل بما يقرِّبه إلى الله وإلى جنته فناسَب أن يذكُر الرَّحمة، فإذا خرج فإنه ينطلق في الدنيا بما فيها من ملهيات ومشغلات، فناسب أن يطلب من الله الحماية والحفظ من الشيطان.
"When any of you enters the mosque, let him say: 'Allahumma iftah li abwāba rahmatik' (O Allah, open for me the gates of Your mercy). And when he leaves, let him say: 'Allahumma inni as’aluka min fadlik' (O Allah, I ask You of Your bounty)."
" إِذَا دَخَلَ أَحَدُكُمُ الْمَسْجِدَ فَلْيُسَلِّمْ عَلَى النَّبِيِّ ـ صلى الله عليه وسلم ـ ثُمَّ لْيَقُلِ اللَّهُمَّ افْتَحْ لِي أَبْوَابَ رَحْمَتِكَ . وَإِذَا خَرَجَ فَلْيَقُلِ اللَّهُمَّ إِنِّي أَسْأَلُكَ مِنْ…