{-# LANGUAGE TupleSections, OverloadedStrings #-} module UtilEmail where import Import import qualified Data.Text.Lazy.Encoding import Network.Mail.Mime (Part(..), Encoding(..), Address(..), Mail(..), emptyMail, renderSendMail, randomString) import Text.Blaze.Renderer.Utf8 (renderHtml) import Text.Hamlet (shamlet) import Text.Shakespeare.Text (stext) import qualified Data.Text as T import System.Random (newStdGen) sendVerifyEmail :: Text -> Text -> Text -> Handler () sendVerifyEmail email _ verurl = liftIO $ renderSendMail (emptyMail $ Address (Just "PFIF") "noreply@protocolfreedom.org") { mailTo = [Address Nothing email] , mailHeaders = [ ("Subject", "Verify your email address") ] , mailParts = [[textPart, htmlPart]] } where textPart = Part { partType = "text/plain; charset=utf-8" , partEncoding = None , partFilename = Nothing , partContent = Data.Text.Lazy.Encoding.encodeUtf8 [stext| Please confirm your email address by visiting the link below. ( #{verurl} ) Thank you, PFIF |] , partHeaders = [] } htmlPart = Part { partType = "text/html; charset=utf-8" , partEncoding = None , partFilename = Nothing , partContent = renderHtml [shamlet|

Please confirm your email address by clicking on the link below.

#{verurl}

Thank you |] , partHeaders = [] } randomKey :: IO Text randomKey = do stdgen <- newStdGen return $ T.pack $ fst $ randomString 10 stdgen