Yes, the internal intention matters, but what I'm arguing is that in intending the cause you implicitly intend the effect also--as per my gun analogy (which I must admit I didn't articulate all that well).
So performing the Sacramental rite according to the form prescribed by the Church is the instrumental cause of the Sacrament. So if you intend (internally) to perform the rite according to the Church's prescription, then you implicitly intend the effect. If you pull a trigger on a loaded gun that's pointed at someone, then you can't say, "Well, I didn't mean to shoot the person but just wanted to pull the trigger." In pulling the trigger you implicitly intended to shoot the person. In other words, my contention is that you cannot simply will away the Sacramental effect any more than you can pull the trigger and will away its effect of shooting the person at which the gun is aimed.
So IMO if some evil freemason priest puts on his vestments, goes to the altar at some scheduled time, performs the rites prescribed by the Church for Mass, says the words of consecration, he's intending to DO WHAT the Church DOES.
That's why an atheist can validly baptize, because one need not actually intend the Sacramental effect.