Very small DIY electromagnet

An air gap of 3 to 4 mm is a nearly 'infinite' distance for a magnetic field, because the field decreases reciprocal to the square of the distance.

Even using an iron core wouldn't change this, it would just increase the static retention force of the magnet.

You could use a 'pot magnet', or more general a 'charchteristic curve affected magnet' (Attention: Two direct german translations ;-). They are quite difficult to design.

The second alternative is an 'moving coil actuator' (the direct german translation is really funny: diving coil actuator ;-). Most probably you know the principle very well, because it's driving ordinary audio speakers. This would be my recommendation. Maybe you can take one from a bass speaker.