Sunan Ibn Majah · Hadith 773
The Book On The Mosques And The Congregations - كتاب المساجد والجماعات
حدثنا محمد بن بشار، حدثنا أبو بكر الحنفي، حدثنا الضحاك بن عثمان، حدثني سعيد المقبري، عن أبي هريرة، أن رسول الله صلى الله عليه وسلم قال " إذا دخل أحدكم المسجد فليسلم على النبي صلى الله عليه وسلم وليقل اللهم افتح لي أبواب رحمتك وإذا خرج فليسلم على النبي وليقل اللهم اعصمني من الشيطان الرجيم " .
It was narrated from Abu Hurairah that: The Mesenger of Allah said: "When anyone of enters the mosque, let him send peace upon the Prophet, then let him say: 'Allahumma aftahli abwaba rahmatik (O Allah, open to me the gates of Your mercy).' And when he leaves, let him send peace upon the Prophet and say: 'Allahumma- simni minash-shaitanir-rajim (O Allah, protect me from the accursed Shaitan).'"
Gradings are classical jarḥ-wa-taʿdīl, name-matched to a rijāl index (not individually re-verified).