The engine requires the right amount of air and fuel to mix together for ignition.This might cause the engine to still start, but it won't be able to keep it running. That is why it will die almost immediately after it starts.
You may be dealing with a vacuum leak or a faulty sensor like a MAP or MAF sensor. You may also have a problem in the fuel system or with the fuel pump. Have the pump checked before replacing it.
Once the transmission fluid is checked most common faults are the torque converter, Idle air control valve, vacuum leaks, or a wiring harness fault. A qualified Technician such as one from YourMechanic will be able to diagnose your engine shuts off when put in drive and make any repairs required.
1) Fuel Pump Leak
The engine requires the right amount of air and fuel to mix together for ignition. But if there is a fuel leak somewhere, then the proper amount of fuel won't be able to make it to the combustion chamber. This might cause the engine to still start, but it won't be able to keep it running.
If you have an automatic transmission vehicle, stalling can be a sign of a malfunctioning torque converter or a torque converter solenoid. The torque converter is the equivalent of the clutch in a manual transmission vehicle. Another potential transmission-related cause of car stall is low transmission fluid levels.