1 -- |Email functions and data types.
6 import Control.Concurrent
7 import Control.Exception (evaluate)
8 import Data.List (intercalate)
11 import System.Time (CalendarTime(..), ClockTime, getClockTime, Month, toCalendarTime)
16 -- |A crude model of an RFC821 email message.
17 data Message = Message { headers :: [Header],
24 default_headers :: [Header]
25 default_headers = ["MIME-Version: 1.0",
26 "Content-type: text/plain; charset=UTF-8"]
28 -- |Showing a message will print it in roughly RFC-compliant
29 -- form. This form is sufficient for handing the message off to
31 instance Show Message where
33 concat [ if (length (headers m) == 0) then "" else (intercalate "\n" (headers m)) ++ "\n",
34 "Subject: " ++ (subject m) ++ "\n",
35 "From: " ++ (from m) ++ "\n",
36 "To: " ++ (to m) ++ "\n",
42 -- |Pad a string on the left with zeros until the entire string has
44 pad_left :: String -> Int -> String
46 | n < (length str) = str
47 | otherwise = (replicate num_zeros '0') ++ str
48 where num_zeros = n - (length str)
52 -- |Formats a month name according to RFC822.
53 format_month :: Month -> String
54 format_month month = take 3 (show month)
57 -- |Takes an offset from UTC (in seconds) and returns the four-digit
58 -- offset as a 'String' in +hhmm format.
59 format_timezone :: Int -> String
60 format_timezone seconds =
61 sign ++ pad_hh ++ pad_mm
63 seconds_norm = abs seconds
64 hh = seconds_norm `div` 3600
65 mm = (seconds_norm - (hh*3600)) `div` 60
66 pad_hh = pad_left (show hh) 2
67 pad_mm = pad_left (show mm) 2
68 sign = if seconds < 0 then "-" else "+"
71 -- |Takes a 'ClockTime' as an argument, and formats it as an RFC822 Date header.
75 -- <http://cr.yp.to/immhf/date.html>
78 format_clocktime :: ClockTime -> IO String
79 format_clocktime ct = do
80 caltime <- (toCalendarTime ct)
82 let days = pad_left (show (ctDay caltime)) 2
83 let month = format_month (ctMonth caltime)
84 let year = show $ ctYear caltime
85 let hours = pad_left (show (ctHour caltime)) 2
86 let minutes = pad_left (show (ctMin caltime)) 2
87 let seconds = pad_left (show (ctSec caltime)) 2
88 let timezone = format_timezone (ctTZ caltime)
90 return $ concat [(show $ ctWDay caltime) ++ ", ",
100 -- |Constructs an RFC822 Date header with the current date/time.
101 construct_date_header :: IO String
102 construct_date_header = do
104 format_clocktime date
108 -- |Takes a message as an argument, and passes it to the system's
110 sendmail :: Message -> IO (String, String, ExitCode)
111 sendmail message = do
112 let sendmail_args = ["-f",
115 (inh, outh, errh, ph) <-
116 runInteractiveProcess "/usr/bin/sendmail" sendmail_args Nothing Nothing
119 outs <- hGetContents outh
122 errs <- hGetContents errh
124 _ <- forkIO $ hPutStr inh (show message) >> hClose inh
125 _ <- forkIO $ evaluate (length outs) >> putMVar outm ()
126 _ <- forkIO $ evaluate (length errs) >> putMVar errm ()
131 ec <- waitForProcess ph
132 return (outs, errs, ec)
135 -- |The 'sendmail' function returns a three-tuple of its outputs,
136 -- errors, and exit codes. This function pretty-prints one of those
138 print_sendmail_result :: (String, String, ExitCode) -> IO ()
139 print_sendmail_result (outs, errs, ec) = do
141 ExitSuccess -> return ()
142 _ -> putStrLn $ concat ["Output: " ++ outs,
143 "\nErrors: " ++ errs,
144 "\nExit Code: " ++ (show ec)]