Concerning the shmira of shem Shakai and the shmira of shem Havaya of the mezuzah.
There are several passages in Zohar Hakadosh ואתחנן that are first makor for writing the shem Shakai on the outside of the mezuzah. In chelek gimel 266 A, it states: “when bad things (sheidim) draw close to a persons door, they lift up their head (since they are compared to a snake they have to “lift up their heads” to see it. -Ramaz) and see the holy name on the outside, that is Shakai, as it is crowned, and rules over all of them, they become fearful and run away. R’ Yitzchak asks, if so, why do you need the whole parsha, just have the name?! In answer, it is specifically when the name is “crowned” by the parsha(s) it is as a king who comes out flanked by his entire army... it is then when they fear him. Come and see that the name והיה is spelled from bottom up (the tziruf is not the order of Shem Havaya) so Shakai is on the outside corresponding to this name, והיה is on the inside while Shakai is on the outside. Thus a person is guarded from all sides, inside and out.”
The Chida explains here, in נצוצי אורות, based on the Raya Mehemna on 263 B, that there is both an interal, as well as an external, concept of guarding here. There is an internal guarding from the Yetzer hara (from have Shem Havaya or, the tziruf והיה) on the inside and guarding your physical body on the outside (from shem Shakai.) There is a nice remez that the words שד״י and והיה together are gematria שם. So the שם ה׳ hints to to the fact that it is actually shem Shakai and Vehaya that are referred to in this pasuk.
The concept of Hashem guarding us is called a רשימה קדישה - a holy covenant. Commensurate with us “guarding” his covenant, Hashem guards us. (This us reminiscent of the medrash, “my candle is in your hand and your candle is in my hand. Guard my candle so I may guard yours...”)
The Zohar then brings a number of other examples of things that are שמירת הברית, just like the mezuzah, among them is the head Tefilin. As Likutei Levi Yitzchok points out on this passage, concerning the Tefilin it states, that “the name of Hashem is upon you - and they will fear from you.” The simple implication of this, ar first glance, is that it is the name Havaya of the Tefilin (on the inside) that protects you. However, Likutei Levi Yitzhok top of דף תנה implies that “the name of Hashem is called upon you” is the Shakai, in the Shin, Daled and Yud knots of the retzuoz, and that is what is associated with the shmira.
According to the Ramaz, R’ Yitzchak’s question is not to just write the name Shakai. Rather, “perhaps you should write it in between the two parshas of Shma and Vehaya?” (That is, that Shakai is the level of Yesod that units the two concepts of kindness and strictness that represent the first and second parsha.) However, in answer, “the name Shakai must be opposite the beginning of the second Parsha since the main point is to guard the feminine aspect (Rachel) that is composed mostly from ‘strictness’.”
Rav Chaim Vital brings the perush of the Arizal on this passage in שער מאמרי רשב״י who is doresh this on the pasuk והיה שד״י בצריך - Iyov 22:25 literally, “shakai will be amongst your enemies.” However, here the drasha is on the word צריך from the language of צירי דלתות - door hinges (נו״ה דאמא) and the two hinges are the two שדיים “breasts” of the supernal kabalistic concept of the nurturing “mother” who watches over her children. Opposite these two ideas in the realm of holiness, are the two bad שדים - “demons,” who are at the door (the שד on the right and side and the Yezter hara on the left) that the mezuzah protects you from.
Now on 263 B, in Raya Mehemna, and on 266 B, on the bottom in the statement of R’ Aba, it brings a similar drasha about the Sheid in the doorway, however with out the specific mention that it is the name Shakai that is connected to protection. In fact, the drasha is on the verse, “Hashem (Havaya) will guard you comings and goings.” We see that the drasha is on the the words שם השם. This might imply that it is action the shem Havaya that is connected to the shmira. However, the Ramak explains that the רשימה קדישה mentioned on 266 B refers to shem Shakai (מובא באור החמה.)
The Ramaz agrees with this, that the concept here of that the meaning of “shem Hashem guards your goings” is referring to shem Shakai and not shem Havaya (or its tziruf, והיה, alone.) The opinion of the Ramaz is that the two parshas are a concept of union of the kabalistic concepts of Aba and Ima and their two יסודות which corresponds to the name Shakai. The union of Aba and Ima is constant זיווגיהו תדיר, hinted to by the the blessing “to afix” a mezuzah.