aboutsummaryrefslogtreecommitdiff
path: root/src/Mail.hs
blob: 45962a5e71e962d0f09b459958ecb6537a1ceacf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{-# 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 }