Expanding foam is a space filler and insulator. It will not secure a door.
You'll need to fix the frame in place with frame fixings, available from your local hardware store.
You'll need at least a 150mm (6 inch) fixings so you get at least 100mm (4 inch) into the masonry surround.
When using frame fixings drill a hole with enough clearance to insert the rawlplug and screw.
Hold the frame in place using wooden wedges and drill through the frame and into the brickwork.
Put the screw into the rawlplug and hand tighten.
Insert the rawlplug containing the screw into the hole and tap into place with a hammer until the flange on the rawlplug is just proud of the frame. Screw the screw home.
You can buy screw caps in different colours to hide the screw heads.
I suggest you use 4 screws each side and 2 top & bottom.
Big Softy
March 2007