1 -- |Email functions and data types.
6 import Control.Concurrent
7 import Control.Concurrent.MVar
8 import Control.Exception (evaluate)
9 import Data.List (intercalate)
16 -- |A crude model of an RFC821 email message.
17 data Message = Message { headers :: [Header],
24 -- |Showing a message will print it in roughly RFC-compliant
25 -- form. This form is sufficient for handing the message off to
27 instance Show Message where
29 concat [ if (length (headers m) == 0) then "" else (intercalate "\n" (headers m)) ++ "\n",
30 "Subject: " ++ (subject m) ++ "\n",
31 "From: " ++ (from m) ++ "\n",
32 "To: " ++ (to m) ++ "\n",
37 -- |Takes a message as an argument, and passes it to the system's
39 sendmail :: Message -> IO (String, String, ExitCode)
41 let sendmail_args = ["-f",
44 (inh, outh, errh, ph) <-
45 runInteractiveProcess "/usr/bin/sendmail" sendmail_args Nothing Nothing
48 outs <- hGetContents outh
51 errs <- hGetContents errh
53 forkIO $ hPutStr inh (show message) >> hClose inh
54 forkIO $ evaluate (length outs) >> putMVar outm ()
55 forkIO $ evaluate (length errs) >> putMVar errm ()
60 ec <- waitForProcess ph
61 return (outs, errs, ec)
64 -- |The 'sendmail' function returns a three-tuple of its outputs,
65 -- errors, and exit codes. This function pretty-prints one of those
67 print_sendmail_result :: (String, String, ExitCode) -> IO ()
68 print_sendmail_result (outs, errs, ec) = do
70 ExitSuccess -> return ()
71 _ -> putStrLn $ concat ["Output: " ++ outs,
73 "\nExit Code: " ++ (show ec)]