-- Assumes email maps to a valid user.
sendVerificationEmail :: Text -> Handler ()
sendVerificationEmail email = do
    Entity userId _ <- fmap fromJust $ runDB $ getBy $ UniqueEmail email
    -- Generate new verKey 
    stdgen <- liftIO newStdGen
    let verKey = T.pack $ fst $ randomString 20 stdgen
    runDB $ do
        mEmailVer <- getBy $ UniqueUserEmailVer userId 
        case mEmailVer of
            -- Insert new verification record.
            Nothing -> do
                _ <- insert $ EmailVerification userId verKey
                return ()
            -- Update a previous record with new verKey.
            Just (Entity evId  _) -> do
                update evId [EmailVerificationVerKey =. verKey]
                return ()
    -- Generate verUrl and create and send email.
    render <- getUrlRender
    tm <- getRouteToMaster
    let verUrl = render $ tm $ VerifyR userId verKey
        to = Address Nothing email
        from = noreplyAddr
        subject = "Bannerstalker verification email"
        text = toLazyText $ $(textFile "templates/verification.text") ()
        html = LT.pack $ renderHtml
            $(shamletFile "templates/verification.hamlet")
    liftIO $ simpleMail to from subject text html [] >>= mySendmail
sendVerificationEmail :: Text -> Handler ()
sendVerificationEmail email = do
    Entity userId _ <- fmap fromJust $ runDB $ getBy $ UniqueEmail email
    -- Generate new verKey 
    stdgen <- liftIO newStdGen
    let verKey = T.pack $ fst $ randomString 20 stdgen
    runDB $ do
        mEmailVer <- getBy $ UniqueUserEmailVer userId 
        case mEmailVer of
            -- Insert new verification record.
            Nothing -> do
                _ <- insert $ EmailVerification userId verKey
                return ()
            -- Update a previous record with new verKey.
            Just (Entity evId  _) -> do
                update evId [EmailVerificationVerKey =. verKey]
                return ()
    -- Generate verUrl and create and send email.
    render <- getUrlRender
    tm <- getRouteToMaster
    let verUrl = render $ tm $ VerifyR userId verKey
        to = Address Nothing email
        from = noreplyAddr
        subject = "Bannerstalker verification email"
        text = toLazyText $ $(textFile "templates/verification.text") ()
        html = LT.pack $ renderHtml
            $(shamletFile "templates/verification.hamlet")
    liftIO $ simpleMail to from subject text html [] >>= mySendmail
sendVerificationEmail email = do
    Entity userId _ <- fmap fromJust $ runDB $ getBy $ UniqueEmail email
    -- Generate new verKey 
    stdgen <- liftIO newStdGen
    let verKey = T.pack $ fst $ randomString 20 stdgen
    runDB $ do
        mEmailVer <- getBy $ UniqueUserEmailVer userId 
        case mEmailVer of
            -- Insert new verification record.
            Nothing -> do
                _ <- insert $ EmailVerification userId verKey
                return ()
            -- Update a previous record with new verKey.
            Just (Entity evId  _) -> do
                update evId [EmailVerificationVerKey =. verKey]
                return ()
    -- Generate verUrl and create and send email.
    render <- getUrlRender
    tm <- getRouteToMaster
    let verUrl = render $ tm $ VerifyR userId verKey
        to = Address Nothing email
        from = noreplyAddr
        subject = "Bannerstalker verification email"
        text = toLazyText $ $(textFile "templates/verification.text") ()
        html = LT.pack $ renderHtml
            $(shamletFile "templates/verification.hamlet")
    liftIO $ simpleMail to from subject text html [] >>= mySendmail