This is a small device called a reed switch. When the device is exposed to a magnetic field, the two ferrous materials inside the switch pull together and the switch closes. When the magnetic field is removed, the reeds separate and the switch opens. This makes for a great non-contact switch. This switch can carry up to 1A.
This reed switch can be populated easily on a PCB and is encapsulated so it's more durable than glass reed switches.