Refactors common mail sending code in spaces and users endpoints

[#75059530]