esp8266 deepsleep

Hi, I have tried to make this work but no success. I have door sensor on my front door. When the reed switch is open I receive email. Code works perfectly but now I want to make device run with battery so I need to get the device go to deep sleep mode to save power. I cant get code work anymore. Device uses esp8266 chip with reed connected to pins d4 and 3v3. Can somebody help to make this work.

How to add deepsleep her?
This is the code without deepsleep:

#include <ESP8266WiFi.h>

const char* ssid = "xxx";
const char* password = "xxx";
const char* host = "xxx";
const char* apiKey = "xxx";

int pin = 2;
volatile int state = false;
volatile int flag = false;
const char* door_state = "closed";

unsigned long previousMillis = 0;
const long interval = 3000;

void changeDoorStatus() {

unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;

state = !state;
if(state) {
door_state = "opened";
}
else{
door_state = "closed";
}
flag = true;
Serial.println(state);
Serial.println(door_state);
}

}

void setup() {
Serial.begin(115200);
delay(100);
Serial.println("Preparing the Door Status Monitor project...");

pinMode(pin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(pin), changeDoorStatus, CHANGE);

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {

if(flag){
Serial.print("connecting to ");
Serial.println(host);

WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}

String url = "/trigger/door_status/with/key/";
url += apiKey;

Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + door_state + "\r\n");
flag = false;
}
delay(10);
}

Perhaps adding a simple switch in order to power the assembly only if the door is open may solve this problem outside coding environment.