aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoris2015-11-21 19:02:49 +0100
committerJoris2015-11-21 19:02:54 +0100
commit7acd7a42f7663aa79d18e24bdb9fe19bf15f8fae (patch)
tree000d1d157ff33917358ded8100980d018e437f7f /src
parent6850159bf998a209ad7b6d7103d113a54659937e (diff)
Log details about sent mails in a file
Diffstat (limited to 'src')
-rw-r--r--src/Logger.hs22
-rw-r--r--src/Main.hs37
-rw-r--r--src/SendMail.hs13
3 files changed, 58 insertions, 14 deletions
diff --git a/src/Logger.hs b/src/Logger.hs
new file mode 100644
index 0000000..4be55be
--- /dev/null
+++ b/src/Logger.hs
@@ -0,0 +1,22 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Logger
+ ( info
+ ) where
+
+import Data.Text (Text)
+import qualified Data.Text as T
+import qualified Data.Text.IO as T (appendFile)
+import Data.Time.LocalTime (getZonedTime)
+import Data.Time.Format (formatTime, defaultTimeLocale)
+
+info :: Text -> IO ()
+info message = do
+ time <- T.pack <$> formatTime defaultTimeLocale "[%d/%m/%Y, %H:%M]" <$> getZonedTime
+ T.appendFile "logs" $
+ T.concat
+ [ time
+ , " - "
+ , message
+ , "\n"
+ ]
diff --git a/src/Main.hs b/src/Main.hs
index d72bd95..0aa2910 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -6,6 +6,7 @@ module Main
import System.IO (stderr)
+import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
@@ -28,24 +29,32 @@ main = do
eitherConfig <- getConfig configPath
case (eitherBirthdates, eitherConfig) of
(Left err, _) ->
- T.hPutStr stderr $
- T.concat
- [ "Error while parsing file "
- , T.pack birthdatePath
- , ":\n"
- , err
- ]
+ birthdateError err
(_, Left err) ->
- T.hPutStr stderr $
- T.concat
- [ "Error while parsing config file "
- , T.pack birthdatePath
- , ":\n"
- , err
- ]
+ configError err
(Right birthdates, Right config) ->
sendNotificationForBirthdayToday birthdates config
+birthdateError :: Text -> IO ()
+birthdateError err =
+ T.hPutStr stderr $
+ T.concat
+ [ "Error while parsing file "
+ , T.pack birthdatePath
+ , ":\n"
+ , err
+ ]
+
+configError :: Text -> IO ()
+configError err =
+ T.hPutStr stderr $
+ T.concat
+ [ "Error while parsing config file "
+ , T.pack configPath
+ , ":\n"
+ , err
+ ]
+
sendNotificationForBirthdayToday :: [Birthdate] -> Config -> IO ()
sendNotificationForBirthdayToday birthdates config = do
currentDate <- getCurrentDate
diff --git a/src/SendMail.hs b/src/SendMail.hs
index 23b1b80..0e1f91d 100644
--- a/src/SendMail.hs
+++ b/src/SendMail.hs
@@ -5,12 +5,25 @@ module SendMail
) where
import Data.Text (Text)
+import qualified Data.Text as T
import Data.Text.Lazy (fromStrict)
import Network.Mail.Mime
+import Logger
+
sendMail :: Text -> Text -> Text -> Text -> IO ()
sendMail to from subject body = do
+ Logger.info $
+ T.concat
+ [ "Sending mail to "
+ , to
+ , " with subject “"
+ , subject
+ , "” and body “"
+ , body
+ , "”"
+ ]
renderSendMail (simpleMail' (address to) (address from) subject (fromStrict body))
address :: Text -> Address