aboutsummaryrefslogtreecommitdiff
path: root/src/Mail.hs
blob: 1b15f30846a746322cc998d33ed43f8ed3978a8f (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
{-# 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)

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 ()))