-
Notifications
You must be signed in to change notification settings - Fork 11
/
Mailer.hs
52 lines (45 loc) · 1.54 KB
/
Mailer.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Mailer
( Email(..)
, sendMail
, easyEmail
, Mailer
, newMailer
) where
import ClassyPrelude
import Control.Lens ((?~), (&))
import Network.Wreq
import Network.HTTP.Client (HttpException)
data Mailer = Mailer { mailerKey :: ByteString }
newMailer :: ByteString -> Mailer
newMailer = Mailer
data Email = Email { emailTo :: Text
, emailFromName :: Text
, emailFromEmail :: Text
, emailReplyTo :: Text
, emailSubject :: Text
, emailBody :: Text
}
easyEmail :: Text -> Text -> Text -> Email
easyEmail to subject body =
Email { emailTo = to
, emailFromName = "Basilica"
, emailFromEmail = "[email protected]"
, emailReplyTo = "[email protected]"
, emailSubject = subject
, emailBody = body
}
emailForm :: Email -> [FormParam]
emailForm Email{..} = [ "to" := emailTo
, "from" := intercalate "" [emailFromName, " <", emailFromEmail, ">"]
, "h:Reply-To" := emailReplyTo
, "subject" := emailSubject
, "text" := emailBody
]
sendMail :: Mailer -> Email -> IO ()
sendMail Mailer{mailerKey} email = catch (void sendEmail) logError
where
sendEmail = postWith opts "https://api.mailgun.net/v3/mail.basilica.horse/messages" form
logError :: HttpException -> IO ()
logError = print
opts = defaults & auth ?~ basicAuth "api" mailerKey
form = emailForm email