{-# LANGUAGE OverloadedStrings #-} module Mail ( sendMail ) where import Data.Text (Text) import Data.Text.Lazy.Builder (toLazyText, fromText) import qualified Data.Text as T import Control.Exception (SomeException, try) import Network.Mail.Mime import Utils.Either (mapLeft) sendMail :: [Text] -> Text -> Text -> IO (Either Text ()) sendMail mailTo subject body = safeSendMail (mail mailTo subject body) safeSendMail :: Mail -> IO (Either Text ()) safeSendMail mail = mapLeft (T.pack . show) <$> (try (renderSendMail mail) :: IO (Either SomeException ())) mail :: [Text] -> Text -> Text -> Mail mail mailTo subject body = let fromMail = emptyMail (address "no-reply@leboncoin-listener.com") in fromMail { mailTo = map address mailTo , mailParts = [[plainPart (toLazyText . fromText $ body)]] , mailHeaders = [("Subject", subject)] } address :: Text -> Address address mail = Address { addressName = Nothing, addressEmail = mail }