--- |Pad a string on the left with zeros until the entire string has
--- length n.
-pad_left :: String -> Int -> String
-pad_left str n
- | n < (length str) = str
- | otherwise = (replicate num_zeros '0') ++ str
- where num_zeros = n - (length str)
-
-
-
--- |Formats a month name according to RFC822.
-format_month :: Month -> String
-format_month month = take 3 (show month)
+-- | Print a 'Message' in roughly RFC-compliant form. This form is
+-- sufficient for handing the message off to sendmail (or compatible).
+--
+-- Examples:
+--
+-- >>> let hs = default_headers
+-- >>> let s = "Save up to 20% on garbage!"
+-- >>> let b = "Just kidding, now you have a virus!"
+-- >>> let f = "savings5000@impenetrable.example"
+-- >>> let t = "everyone@everywhere.example"
+-- >>> let msg = Message hs s b f t
+-- >>> putStrLn $ to_rfc822 msg
+-- MIME-Version: 1.0
+-- Content-Type: text/plain; charset=UTF-8
+-- Content-Transfer-Encoding: 8bit
+-- Subject: Save up to 20% on garbage!
+-- From: savings5000@impenetrable.example
+-- To: everyone@everywhere.example
+-- <BLANKLINE>
+-- Just kidding, now you have a virus!
+--
+to_rfc822 :: Message -> String
+to_rfc822 m =
+ concat [ formatted_headers,
+ "Subject: " ++ (subject m) ++ "\n",
+ "From: " ++ (from m) ++ "\n",
+ "To: " ++ (to m) ++ "\n",
+ "\n",
+ (body m) ]
+ where
+ formatted_headers =
+ if null (headers m)
+ then ""
+ else unlines (headers m)
+
+
+
+-- | Constructs a 'String' in RFC822 date format for the current
+-- date/time.
+--
+rfc822_now :: IO String
+rfc822_now =
+ liftM (formatTime defaultTimeLocale rfc822DateFormat) getZonedTime