The Messenger of Allah ﷺ said, "When one of you enters the mosque, let him send greetings of peace upon the Prophet ﷺ and say, 'Allah, open for me the gates of Your mercy.' And when he leaves, let him send greetings of peace upon the Prophet ﷺ and say, 'Allah, protect me from the accursed Satan.'"
حدثنا محمد بن بشار حدثنا أبو بكر الحنفي حدثنا الضحاك بن عثمان حدثني سعيد المقبري
أن رسول الله ﷺ قال إذا دخل أحدكم المسجد فليسلم على النبي ﷺ وليقل اللهم افتح لي أبواب رحمتك وإذا خرج فليسلم على النبي ﷺ وليقل اللهم اعصمني من الشيطان الرجيم
[Sahih] Nasai narrated it in Al-Kubra, Amal al-Yawm wal-Layla, no. 90, from Ibn Bashar, and its chain is hasan. Busiri said, "This is a sahih chain, its narrators are trustworthy." Ibn Hibban graded it sahih (Mawarid, no. 321), as did Ibn Khuzayma (no. 452), and Hakim (1/207), and Dhahabi. Nasai mentioned a flaw in it that is not damaging.