Whenever the Messenger of Allah ﷺ entered the mosque, he would say, "In the name of Allah, and peace be upon the Messenger of Allah. O Allah, forgive me my sins and open to me the gates of Your mercy."And when he left, he would say, "In the name of Allah, and peace be upon the Messenger of Allah. O Allah, forgive me my sins and open to me the gates of Your bounty."
حدثنا أبو بكر بن أبي شيبة حدثنا إسمعيل بن إبراهيم وأبو معاوية عن ليث عن عبد الله بن الحسن عن أمه
كان رسول الله ﷺ إذا دخل المسجد يقول بسم الله والسلام على رسول الله اللهم اغفر لي ذنوبي وافتح لي أبواب رحمتك وإذا خرج قال بسم الله والسلام على رسول الله اللهم اغفر لي ذنوبي وافتح لي أبواب فضلك
[Its chain of transmission is daif] Tirmidhi narrated it in the Book of Prayer, chapter on what is said upon entering the masjid, hadith 314, from the hadith of Ismail, through this same route, and he said it is a hasan hadith but its chain of transmission is not connected. Layth bin Abu Salim, his status has already been discussed, hadith 208. See also the following hadith, for it suffices in place of this one.