The magic on the Prophet ﷺ is confirmed in the two sahihs (most correct books of hadith), and it is magic that concerns only his relationship with his wives and did not have any effect on his mind, or anything to do with the message, and the messengers were not innocent from trials.
Sheikh ibn Baaz- may Allah have mercy on him- says:
The magic on the Prophet ﷺ was confirmed in the sahih hadith that it happened in Madinah, and when the revelation had become stable, the evidence for prophethood was clear, and the truth of the message was clear. Allah SWA made His Prophet ﷺ victorious over the idolaters, and He made them inferior.
A person from the Jews came to him: his name was Lebaid bin Al A’sam, and he cast a spell on the Prophet ﷺ using a comb and the hairs stuck to it and the spathe of a male date palm. The Prophet ﷺ began to imagine that he had done something when he had not, but his mind, feelings and defining feature were not affected with the grace of Allah SWA, and he used to speak to the people with the truth that Allah SWA has revealed to him, but he felt it had had an effect on the way he was with his wives.
Aaishah RA said: He used to imagine that he had done things in the house with his family and he had not done them, so the revelation came to him from His lord SWA with the messenger Gabriel AS and he told him of what had happened. Then he sent someone to extract the magic from the well of the Ansaar, thereafter the effect it had on him was removed with the grace of Allah SWA. Allah SWA sent down the two surahs of protection, he read them, and all of the calamity was removed, and he said ﷺ those seeking refuge in them no one can use anything to compare with them for the purpose, and he will not be harmed by anything, or what would make him leave the message or revelation, And Allah SWA is infallible from people which prevent the message from being delivered and conveyed.
As for what befalls the prophets in terms of trials, the Prophet was not safe from trial, in fact he had many trials. He was hurt on the day of Uhud, an helmet was used to hit his head, and some links of his chain mail were embedded in his cheek, and he fell in some of the holes there. They hurt him a lot in Makkah, and his heart was tested like all of the prophets before him, and received his fate. Allah raised him in rank because of it, and his status was much higher, and his good deeds were multiplied, but Allah SWA saved him from them and they could not kill him nor could they prevent him from spreading the message and they were unable to come between him and what he was ordered to do. He has actually delivered the message successfully, and spread peace ﷺ.