Not all E39 gearboxes are interchangeable.
When my car had its gearbox overhauled and torque converter replaced, there was a 520i E39 in the shop for a gearbox overhaul as well.
I was told that the E39's, whether it's a 520, 523, 525, 528 or 530, all use the same basic ZF5HP19 gearbox. The only difference was in (1) the final drive ratio and (2) the size of the torque converter.
The most obvious of course is the torque converter. You can physically spot if the gearbox came from a 520 - it's physically smaller than a 525i's. The 525 & 530 share the same gearbox & torque converter set-up.
The 528/523 uses a similarly sized torque converter as the 525 but has a different lock-up clutch configuration from any other model.
I'd recommend sourcing for a gearbox specific to your 520 if replacement is absolutely the only way to fix your car. But if you ask me, a better solution is to try to get your gearbox inspected by a specialist to see if they can overhaul it. It can sometimes prove to be not much more expensive compared to getting a recond box from an unknown donor car with little or no service history.
Good luck with your gearbox, bro!