Praise be to Allah
We ask Allah, may He be exalted, to give you the comfort of being close to Him, for there is no joy like the joy of feeling close to Him; and no one can feel happy with anyone as he will feel happy with Allah; and his heart will never be at ease with anyone as it will be at ease with Allah. Allah, may He be exalted, says (interpretation of the meaning):
Those who have believed and whose hearts are assured by the remembrance of Allah. Unquestionably, by the remembrance of Allah hearts are assured. [ar-Ra‘d 13:28].
So turn to Him, and offer a lot of du‘a (supplication) and beseech Allah, asking Him to relieve your distress and alleviate your loneliness, and to keep you chaste and make you independent of means. Marriage is not the ultimate goal and divorce is not the end of the world. How many married women are in the worst misery and how many divorced women are very happy. The point is that we should be content with what Allah does and what He decrees. When a person is content with Allah, he will attain the pleasure of Allah, and this is true happiness. These are the people of whom Allah says (interpretation of the meaning):
Allah is pleased with them, and they are pleased with Him [al-Mujadilah 58:22].
On our website we have previously answered a question about a situation that was similar to yours, a comprehensive answer for one who is suffering from loneliness and anxiety after divorce and is looking for a solution. Please see the answer to question no. 65922.
Remember that masturbation is haram (impermissible) because of evidence that has been quoted previously in the answer to question no. 329.
But some fuqaha (jurists) allow masturbation if a person fears for his religious commitment or physical well-being, under the heading of committing the lesser of two evils.
Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) was asked about a man whose desire overwhelms him and he masturbates, and he knows that this may be dealt with by fasting but it is too difficult for him.
He replied: With regard to water (semen) that is emitted involuntarily, there is no sin on him for that, but he has to do ghusl (bathe) if the water came out gushing. As for making it happen by choice, by means of masturbation, this is haram according to most scholars, and it is one of the two opinions narrated from Ahmad; rather it is the stronger of the two. According to one report, it is makrooh (disliked), but if he is compelled to do it, such as if he is afraid of committing zina (fornication/adultery) or becoming ill if he does not masturbate, then in this case there are two well-known scholarly opinions. In this case, several of the earlier and later (scholars) granted a concession allowing it, but others forbade it. And Allah knows best.End quote from al-Fatawa al-Kubra, 1/302
Al-Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said: “The one who masturbates unnecessarily should be given a disciplinary punishment.” … “unnecessarily” means without having any need for that. Needs are of two types: spiritual and physical. As for spiritual needs, they are when a person fears that he may fall into zina, such as if he is in a country where it is easy to commit zina, so we say: If his desire becomes too strong, he will either extinguish it with this action (masturbation) or he will go to a place where he can find prostitutes and commit zina. So in this case we say to him: This is a legitimate need, because the principle in sharee‘ah (Islamic law) is to ward off the greater of two evils with the lesser. This is in accordance with sound reasoning. If this person must fulfil his desire, in one way or the other, then in that case we say to him: This action is permissible because it is a case of necessity.
With regard to physical needs, if a person fears physical harm if this accumulated semen is not expelled, because in some people, if their desire becomes strong and this accumulated semen is not expelled, then they may develop psychological problems and hate to mix with people or sit with them…
So if he fears that he may be harmed, then it is permissible for him to do this action, because it is a physical need. If it is not necessary but he does it, he should be given a disciplinary punishment, i.e., a punishment that will serve as a deterrent.
End quote from al-Sharh al-Mumti‘, 14/318
We ask Allah to bless you with guidance and piety, and to make you chaste and independent of means, and to bless you with a righteous husband and offspring.
And Allah knows best.