diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Mail.hs | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/src/Mail.hs b/src/Mail.hs index 5fc2f24..1b15f30 100644 --- a/src/Mail.hs +++ b/src/Mail.hs @@ -5,28 +5,29 @@ module Mail ) 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.Email.Sendmail (sendmail) +import Network.Mail.Mime 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 ())) +sendMail mailTo subject body = safeSendMail (mail mailTo subject body) + +mail :: [Text] -> Text -> Text -> Mail +mail mailTo subject body = + (emptyMail (address "no-reply@leboncoin-listener.com")) + { mailTo = map address mailTo + , mailParts = [[plainPart (toLazyText . fromText $ body)]] + , mailHeaders = [("Subject", subject)] + } + +address :: Text -> Address +address mail = Address { addressName = Nothing, addressEmail = mail } + +safeSendMail :: Mail -> IO (Either Text ()) +safeSendMail mail = + mapLeft (T.pack . show) <$> (try (renderSendMail mail) :: IO (Either SomeException ())) |