diff options
author | Joris | 2015-11-21 19:02:49 +0100 |
---|---|---|
committer | Joris | 2015-11-21 19:02:54 +0100 |
commit | 7acd7a42f7663aa79d18e24bdb9fe19bf15f8fae (patch) | |
tree | 000d1d157ff33917358ded8100980d018e437f7f | |
parent | 6850159bf998a209ad7b6d7103d113a54659937e (diff) |
Log details about sent mails in a file
-rw-r--r-- | logs | 2 | ||||
-rw-r--r-- | src/Logger.hs | 22 | ||||
-rw-r--r-- | src/Main.hs | 37 | ||||
-rw-r--r-- | src/SendMail.hs | 13 |
4 files changed, 60 insertions, 14 deletions
@@ -0,0 +1,2 @@ +[21/11/2015] - Sending mail to joris.guyonvarch@gmail.com with subject “Hey, there is 1 birthday today!” and body “Alain Noe is now 19 years old.” +[21/11/2015, 18:55] - Sending mail to joris.guyonvarch@gmail.com with subject “Hey, there is 1 birthday today!” and body “Alain Noe is now 19 years old.” 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 |