summaryrefslogtreecommitdiff
path: root/src/Data/CSV/Table/Email.hs
blob: d70ac290cb89eda809c6237fd9cf50b0195fe6b2 (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
35
36
37
38
39
40
41
42
43
module Data.CSV.Table.Email
  ( -- * Email representation
    Email (..)

    -- * Send function
    , sendMail

   ) where

import Text.Printf            (printf)
import System.Process
import Control.Monad          (forM_)
import Data.List              (intercalate)
import Data.CSV.Table.Types
import Data.CSV.Table.Ops


data Email   = E { uid     :: String
                 , to      :: String
                 , cc      :: [String]
                 , sender  :: String
                 , subject :: String
                 , text    :: String
                 , send    :: Bool
                 } deriving (Show)

sendMail      :: Table -> (RowInfo -> Email) -> IO ()
sendMail t f  = forM_ (mapRows f t) sendMail1

sendMail1 :: Email -> IO ()
sendMail1 e = do
  let tmp  = uid e
  let cmd  = mailCmd e
  writeFile tmp (text e)
  status  <- if send e then show `fmap` system cmd else return "FAKESEND"
  putStrLn $ printf "[exec: %s] Status[%s]: %s %s" cmd status (uid e) (to e)

mailCmd :: Email -> String
mailCmd e =
    printf "mail -s \"%s\" -aFrom:%s %s %s < %s" (subject e) (sender e) (to e) (ccs $ cc e) (uid e)
  where
    ccs [] = ""
    ccs xs = "-c " ++ intercalate "," xs