{-# LANGUAGE OverloadedStrings #-} module Mail ( sendMail ) where import Data.Text (Text) import qualified Data.Text as T import Control.Exception (SomeException, try) import Network.Email.Sendmail (sendmail) import Utils.Either (mapLeft) sendMail :: [Text] -> Text -> Text -> IO (Either Text ()) sendMail mailTo subject message = let from = Just "no-reply@leboncoin-listener.com" in safeSendMail from (map T.unpack $ mailTo) (T.unpack $ makeBody subject message) makeBody :: Text -> Text -> Text makeBody subject message = T.concat [ "Subject: " , subject , "\n\n" , message ] safeSendMail :: Maybe String -> [String] -> String -> IO (Either Text ()) safeSendMail from to body = mapLeft (T.pack . show) <$> (try (sendmail from to body) :: IO (Either SomeException ()))