All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger.
First of all, we ask Allaah to facilitate your matter and enable you to that which is beneficial for your religion and your worldly matters, and bless you with a lawful income that will suffice you from resorting to unlawful earnings.
If the nature of this work necessitates sending forbidden matters, then this job is forbidden as it is helping in sin. Allaah Says (what means): {Help you one another in virtue, righteousness and piety but do not help one another in sin and transgression.}[Quran 5:2]
Therefore, you are obliged to leave this job and it is not permissible for you to continue doing it except for a necessity if you do not find another lawful job. In which case, you are permitted to continue doing this job according to necessity, and once you find a lawful job, you are obliged to leave the forbidden job as you are no longer in necessity.
However, you should know that the earth of Allaah is spacious; if you do not find a lawful job in the country where you are, then Allaah may facilitate for you a job in another country. Therefore, you should endeavour to this effect and continue searching. Allaah Says (what means): {… And whosoever fears Allaah and keeps his duty to Him, He will make a way for him to get out (from every difficulty). And He will provide him from (sources) he never could imagine.}[Quran 65:2-3]
As regards the money that you earned from this forbidden job and which you had already spent, then you are not sinful to have done that, but whatever money remains with you, you are obliged to get rid of a portion of the salary according to the forbidden service in your work. You should estimate a fixed percentage and if it is not possible to determine a fixed percentage, then you may approximate it according to what you predominantly think. However, if you need a portion of this forbidden percentage in order to fulfil your basic needs and the needs of the people you are obliged to spend on, then it is permissible for you to take from it according to your necessity or dire need and you are obliged to get rid of the rest.