The Shulchan Aruch · Yoreh De'ah
יורה דעה רפ״הYoreh De'ah 285
Siman 285 of 402 in Yoreh De'ah — 2 se'ifim
Se’if 1א׳
מִצְוַת עֲשֵׂה לִכְתֹּב פ' שְׁמַע (דְּבָרִים ו, ד ט) וְהָיָה אִם שָׁמוֹעַ (דְּבָרִים יא, יג כא) וּלְקָבְעָם עַל מְזוּזַת הַפֶּתַח, וְצָרִיךְ לִזָּהֵר בָּהּ מְאֹד; וְכָל הַזָּהִיר בָּהּ, יַאֲרִיכוּ יָמָיו וִימֵי בָּנָיו. וְאִם אֵינוֹ זָהִיר בָּהּ, יִתְקַצְּרוּ. הַגָּה: וּמִכָּל מָקוֹם אִם אֵין יָדוֹ מַשֶּׂגֶת לִקְנוֹת תְּפִלִּין וּמְזוּזָה, יִקְנֶה תְּפִלִּין וְלֹא מְזוּזָה (יְרוּשַׁלְמִי סוֹף מְגִלָּה ובא"ח סי' נ"ח סי"ב), דְּמִצְוָה שֶׁהִיא חוֹבַת הַגּוּף עֲדִיפָא.
The reward and mitzva of mezuzah. Containing 2 paragraphs: It is a positive commandment to write the Shema, the Veyhaya Im-Shamoa, and to fix them on the side of one’s door, and one needs to be very careful in this, and all who are careful in this, their days will be lengthened, and the days of their children. And if one is not careful in this, his days will be shortened. Rama: And in any case, if it's out of his reach to acquir Tefillin and a Mezuzah, let him acquire Tefillin and not Mezuzah (Yerushalmi Megillah, end; O"C 38:12), as a mitzvah that is a bodily obligation is greater.
Se’if 2ב׳
נְתִינָתָהּ בַּטֶּפַח הַחִיצוֹן. הַגָּה: יֵשׁ אוֹמְרִים כְּשֶׁאָדָם יוֹצֵא מִן הַבַּיִת יַנִּיחַ יָדוֹ עַל הַמְּזוּזָה (מהרי"ל שָׁם וּמוּכָח בַּעֲבוֹדַת כּוֹכָבִים דַּף י"א), וְיֹאמַר: ה' יִשְׁמֹר צֵאתִי וְכו' (בַּמִּדְרָשׁ). וְכֵן כְּשֶׁיִּכָּנֵס אָדָם לְבַיִת, יַנִּיחַ יָדוֹ עַל הַמְּזוּזָה.
Its placement is in the outer tefah. Rama: There are those who say that when a person leaves his house, he puts his hand on the Mezuzah there and says “God will guard my going, etc.” And so too when one enters a house, one puts his hand on the Mezuzah.
Shulchan Aruch, Yoreh De'ah — Torat Emet 357 edition, via Sefaria — public domain
English: Sefaria Community Translation — CC0