endEmail :: LazymailCurses ()
sendEmail = do
  st <- get
  cfg <- ask
  let cs = composeState st
  if not . readyToSend $ cs
    then drawNotification $
         "The email is not ready to be sent. Please check that all fields are correct."
    else do
      exitStatus <- liftIO $ do
        emailHandle <- prepareEmail cs
        child <- runProcess (head . sendmailCommand $ cfg)
                            (tail . sendmailCommand $ cfg)
                            Nothing Nothing (Just emailHandle) Nothing Nothing
        e <- waitForProcess child
        hClose emailHandle
        return e
      handleExitStatus exitStatus

  where
    handleExitStatus ExitSuccess = do
      drawNotification $ "The email was successfully sent."
      st <- get
      put $ st { mode = MaildirMode, composeState = initialComposeState }
    handleExitStatus _ = drawNotification $
                         "Could not send the email. Please, check the logs of for your SMTP client."
    prepareEmail cs = do
      let fs = composeFields cs
      let fileName = (maybe "" id $ bodyFileName cs)
      body <- (Strict.hGetContents =<< openFile fileName ReadMode)
      emailHandle <- openFile fileName WriteMode
      hPutStrLn emailHandle $ (unlines . ppComposeFields True $ fs) ++ body
      hClose emailHandle >> openFile fileName ReadMode

sendEmail :: LazymailCurses ()
sendEmail = do
  st <- get
  cfg <- ask
  let cs = composeState st
  if not . readyToSend $ cs
    then drawNotification $
         "The email is not ready to be sent. Please check that all fields are correct."
    else do
      exitStatus <- liftIO $ do
        emailHandle <- prepareEmail cs
        child <- runProcess (head . sendmailCommand $ cfg)
                            (tail . sendmailCommand $ cfg)
                            Nothing Nothing (Just emailHandle) Nothing Nothing
        e <- waitForProcess child
        hClose emailHandle
        return e
      handleExitStatus exitStatus

  where
    handleExitStatus ExitSuccess = do
      drawNotification $ "The email was successfully sent."
      st <- get
      put $ st { mode = MaildirMode, composeState = initialComposeState }
    handleExitStatus _ = drawNotification $
                         "Could not send the email. Please, check the logs of for your SMTP client."
    prepareEmail cs = do
      let fs = composeFields cs
      let fileName = (maybe "" id $ bodyFileName cs)
      body <- (Strict.hGetContents =<< openFile fileName ReadMode)
      emailHandle <- openFile fileName WriteMode
      hPutStrLn emailHandle $ (unlines . ppComposeFields True $ fs) ++ body
      hClose emailHandle >> openFile fileName ReadMode
sendEmail = do
  st <- get
  cfg <- ask
  let cs = composeState st
  if not . readyToSend $ cs
    then drawNotification $
         "The email is not ready to be sent. Please check that all fields are correct."
    else do
      exitStatus <- liftIO $ do
        emailHandle <- prepareEmail cs
        child <- runProcess (head . sendmailCommand $ cfg)
                            (tail . sendmailCommand $ cfg)
                            Nothing Nothing (Just emailHandle) Nothing Nothing
        e <- waitForProcess child
        hClose emailHandle
        return e
      handleExitStatus exitStatus

  where
    handleExitStatus ExitSuccess = do
      drawNotification $ "The email was successfully sent."
      st <- get
      put $ st { mode = MaildirMode, composeState = initialComposeState }
    handleExitStatus _ = drawNotification $
                         "Could not send the email. Please, check the logs of for your SMTP client."
    prepareEmail cs = do
      let fs = composeFields cs
      let fileName = (maybe "" id $ bodyFileName cs)
      body <- (Strict.hGetContents =<< openFile fileName ReadMode)
      emailHandle <- openFile fileName WriteMode
      hPutStrLn emailHandle $ (unlines . ppComposeFields True $ fs) ++ body
      hClose emailHandle >> openFile fileName ReadMode