Pope Leo XIII said in 1896:
“The Church does not judge about the mind and intention, in so far as it is something by its nature internal; but in so far as it is manifested externally she is bound to judge concerning it. A person who has correctly and seriously used the requisite matter and form to effect and confer a sacrament is presumed for that very reason to have intended to do (intendisse) what the Church does. On this principle rests the doctrine that a Sacrament is truly conferred by the ministry of one who is a heretic or unbaptized, provided the Catholic rite be employed. [Apostolicae Curae 33; emphasis added]”
"Some folks think that Catholic acceptance of any Protestant Baptism at all is a Vatican II novelty. This is not the case. Here is what the Catechism of the Council of Trent says:
Those who may administer Baptism in case of necessity, but without its solemn ceremonies, hold the last place; and in this class are included all, even the laity, men and women, to whatever sect they may belong. This office extends in case of necessity, even to Jews, infidels and heretics, provided, however, they intend to do what the Catholic Church does in that act of her ministry. These things were established by many decrees of the ancient Fathers and Councils; and the holy Council of Trent denounces anathema against those who dare to say, that Baptism, even when administered by heretics, in the name of the Father, and of the Son, and of the Holy Ghost, with the intention of doing what the Church does, is not true Baptism. [source; emphasis added]"