Question: What is the proper way to affix the Mezuzah, vertically or tilted diagonally?
Answer: The Gemara in Masechet Menachot states, “Rav Yehuda said in the name of Rav: If one made it in the form of a carpenter, it is invalid.” Rashi comments, “Carpenters would commonly stick pieces of wood into the wall along the width of the wall.” Based on Rashi, the interpretation of this Gemara is that if one affixes the Mezuzah in a horizontal position, one has not fulfilled one’s obligation. The Rambam as well as Maran Ha’Shulchan Aruch (Yoreh De’ah, Chapter 289) concur with Rashi’s position and rule that the Mezuzah must be affixed vertically along the length of the doorpost, similar to how Sifrei Torah are placed in the Ark standing in a vertical position.
On the other hand, Rabbeinu Tam disagrees with Rashi’s opinion and writes that, on the contrary, it is actually degrading to the Mezuzah to affix it in a vertical position. Thus, according to his opinion, one must affix the Mezuzah in a horizontal position across the width of the doorpost and if one has affixed it vertically, one has not fulfilled one’s obligation.
The Tosafot write that according to both Rashi and Rabbeinu Tam though, if one has affixed the Mezuzah slanted diagonally, one has fulfilled one’s obligation. Indeed, the Rama writes in his gloss on the Shulchan Aruch that the Ashkenazi custom is to affix the Mezuzah on a diagonal slant in order to fulfill one’s obligation according to all opinions.
Nevertheless, according to the letter of the law, the Vilna Gaon writes that even according to the Ashkenazim the Halacha follows Rashi, the Rambam, and Maran Ha’Shulchan Aruch since all of Rabbeinu Tam’s questions on Rashi’s opinion can be answered. Thus, in a situation where the Mezuzah cannot be affixed diagonally, Maran Rabbeinu Ovadia Yosef zt”l writes that even Ashkenazim should affix the Mezuzah in a vertical position, in accordance with the Sephardic custom. However, if one affixes it in a completely horizontal position, one has not fulfilled one’s obligation, even according to the Ashkenazi custom.
Summary: The Mezuzah should be affixed in a completely vertical position, in accordance with the opinion of Rashi. If one affixes it in a horizontal position, one has not fulfilled one’s obligation. The Ashkenazi custom is to preferably affix the Mezuzah slanted diagonally. Nevertheless, even according to their custom, if it is not possible to affix the Mezuzah diagonally, such as if the doorpost is too narrow, the Mezuzah should be affixed vertically.