]> gitweb.michael.orlitzky.com - dead/halcyon.git/blob - src/Mail.hs
2da56639667b4b2b63d387aaab601934091f5f7e
[dead/halcyon.git] / src / Mail.hs
1 -- |Email functions and data types.
2
3 module Mail
4 where
5
6 import Control.Concurrent
7 import Control.Exception (evaluate)
8 import Data.List (intercalate)
9 import Data.Time (formatTime, getZonedTime)
10 import System.Exit
11 import System.Locale (defaultTimeLocale, rfc822DateFormat)
12 import System.Process
13 import System.IO (hClose, hGetContents, hPutStr)
14
15
16 type Header = String
17
18 -- |A crude model of an RFC821 email message.
19 data Message = Message { headers :: [Header],
20 subject :: String,
21 body :: String,
22 from :: String,
23 to :: String }
24 deriving (Eq)
25
26 -- |The default headers attached to each message.
27 -- The MIME junk is needed for UTF-8 to work properly.
28 -- Note that your mail server should support the 8BITMIME extension.
29 default_headers :: [Header]
30 default_headers = ["MIME-Version: 1.0",
31 "Content-Type: text/plain; charset=UTF-8",
32 "Content-Transfer-Encoding: 8bit"]
33
34 -- |Showing a message will print it in roughly RFC-compliant
35 -- form. This form is sufficient for handing the message off to
36 -- sendmail.
37 instance Show Message where
38 show m =
39 concat [ if (length (headers m) == 0) then "" else (intercalate "\n" (headers m)) ++ "\n",
40 "Subject: " ++ (subject m) ++ "\n",
41 "From: " ++ (from m) ++ "\n",
42 "To: " ++ (to m) ++ "\n",
43 "\n",
44 (body m) ]
45
46
47
48 -- |Pad a string on the left with zeros until the entire string has
49 -- length n.
50 pad_left :: String -> Int -> String
51 pad_left str n
52 | n < (length str) = str
53 | otherwise = (replicate num_zeros '0') ++ str
54 where num_zeros = n - (length str)
55
56
57
58 -- | Constructs a 'String' in RFC822 date format for the current
59 -- date/time.
60 rfc822_now :: IO String
61 rfc822_now = do
62 date <- getZonedTime
63 return $ formatTime defaultTimeLocale rfc822DateFormat date
64
65
66
67 -- |Takes a message as an argument, and passes it to the system's
68 -- sendmail binary.
69 sendmail :: Message -> IO (String, String, ExitCode)
70 sendmail message = do
71 let sendmail_args = ["-f",
72 (from message)]
73
74 (inh, outh, errh, ph) <-
75 runInteractiveProcess "/usr/bin/sendmail" sendmail_args Nothing Nothing
76
77 outm <- newEmptyMVar
78 outs <- hGetContents outh
79
80 errm <- newEmptyMVar
81 errs <- hGetContents errh
82
83 _ <- forkIO $ hPutStr inh (show message) >> hClose inh
84 _ <- forkIO $ evaluate (length outs) >> putMVar outm ()
85 _ <- forkIO $ evaluate (length errs) >> putMVar errm ()
86
87 readMVar outm
88 readMVar errm
89
90 ec <- waitForProcess ph
91 return (outs, errs, ec)
92
93
94 -- |The 'sendmail' function returns a three-tuple of its outputs,
95 -- errors, and exit codes. This function pretty-prints one of those
96 -- three-tuples.
97 print_sendmail_result :: (String, String, ExitCode) -> IO ()
98 print_sendmail_result (outs, errs, ec) = do
99 case ec of
100 ExitSuccess -> return ()
101 _ -> putStrLn $ concat ["Output: " ++ outs,
102 "\nErrors: " ++ errs,
103 "\nExit Code: " ++ (show ec)]