diff options
author | Joris Guyonvarch | 2015-04-15 23:47:31 +0200 |
---|---|---|
committer | Joris Guyonvarch | 2015-04-15 23:47:31 +0200 |
commit | 1360b4e49d4ddcf3230e423ca528430d985399c1 (patch) | |
tree | 2f39df3a524387e4319e9a098dba5e25ae043771 /src | |
parent | 4de52f287fd9f0cdae9b6cb55678e85458cbbc04 (diff) |
Using mime-mail instead of MissingH to send mail so that we have a correct encoding
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 ())) |