Need an extension cord with lever or magnetic switch
First off, you're not going to want to use an extension cord for this.  This is all going to have to be done with an approved wiring method. So you'll want to used properly secured and supported wire, that's approved for permanent installation.

Depending on how much space is available, and if you can find a way to mount it.  A door jamb switch, would be one way to go. They're most commonly used, to turn lights on when closet doors open.

The other option, is to use a low voltage circuit to control the line voltage to the device.  With this approach, you'll need a transformer to step power down for the low voltage circuit.  You'll probably find 12V and 24V transformers easily. Then you'll need a relay that can switch the line voltage. You'll also have to make sure that the coil is rated at the low voltage you've chosen to use.

Make sure all you're connections are made in properly installed enclosures, and that all the wiring and devices are installed in accordance with their documentation and the NEC.