6L5BK5EHPAOQX3JCKUJ273UDNAC23LPQL4HIJGM4AV3P3QK5OKIQC
LCBJULKEU4I5FRUGYNRQSHXYLY5X3LRTSNWDCTZLS7YE4BBBDE6AC
TCOAKCGGHOIRJCTZYEZQ3K6KCGL2LGAYGYFRGSPCHBTJJY2V6AXAC
2XQD6KKKD6QVHFHAEMVE3XXY7X2T7BLTLL7XIILZAXNJJH2YONUQC
64C6AWH66FDKU6UE6Z6JPX2J2GBM2JOPTH2GL6LHKAIUBGNGDZ5AC
OBFPJS2GHO2PEHBHGEHKIUOUAFIQHPIZXEVD2YIE3ZIE2PVMH5VAC
GCVQD44VRPQVKPZEPIC4AOIXLJIG2ZMV3QI2Y7KALUT6NVUBSGSAC
M3KUPGZK2UTW4FG3Q632K7P7MI4FVWD5TTIP45UTI3E72UKOWJBAC
QO4NFWIYHF45PF7BA4IYGVZZ7CVZDHIV2427MQ6NXWHLIGBHBQCAC
NVOCQVASZWTKQJG7GPH7KHKZZR7NUG4WLV5YY4KAIRPCJRWCZPIAC
Z7KS5XHHC6PAMTVBHXY7KUSS3BWAOU6FSYIITUCFOOJZU4OUJHBAC
V2VDN77HCSRYYWXDJJ2XOVHV4P6PVWNJZLXZ7JUYPQEZQIH5BZ3QC
PBD7LZYQHXAA3KLH2ZUX5GW4UFML6BQ32KXZF4KZ6OYFASUYFJ5QC
O5FVTOM6YFBLEPF3S576K6IMT6ZZ5VQCSB3YVXNS4CKBITKCRZ7AC
readSendgridAuth :: CT.Config -> IO Sendgrid.Authentication
readSendgridAuth cfg =
Sendgrid.Authentication <$> C.require cfg "sendgridUser"
<*> C.require cfg "sendgridKey"
readSmtpConfig :: CT.Config -> IO SmtpConfig
readSmtpConfig cfg =
SmtpConfig <$> C.require cfg "smtpHost"
<*> ((fmap . fmap) fromInteger $ C.lookup cfg "smtpPort")
<*> C.require cfg "smtpUser"
<*> C.require cfg "smtpKey"
inviteEmail <- liftIO $
projectInviteEmail (templatePath cfg) (p ^. projectName) (u ^. userEmail) toEmail invCode
maybeSuccess <- liftIO $ Sendgrid.sendEmail (sendgridAuth cfg) inviteEmail
maybe
(snapError 500 "The invitation record was created successfully, but the introductory email could not be sent.")
(const $ pure ())
maybeSuccess
liftIO $ sendProjectInviteEmail cfg (p ^. projectName) (u ^. userEmail) toEmail invCode
sendProjectInviteEmail :: QConfig
-> ProjectName
-> Email -- Inviting user's email address
-> Email -- Invitee's email address
-> InvitationCode
-> IO ()
sendProjectInviteEmail cfg pn fromEmail toEmail invCode =
let SmtpConfig{..} = smtpConfig cfg
mailer = maybe (sendMailWithLogin smtpHost) (sendMailWithLogin' smtpHost) smtpPort
in buildProjectInviteEmail (templatePath cfg) pn fromEmail toEmail invCode >>=
(mailer smtpUser smtpPass)
projectInviteEmail :: System.IO.FilePath
-> ProjectName
-> Email -> Email
-> InvitationCode
-> IO Sendgrid.EmailMessage
projectInviteEmail templatePath pn from' to' invCode = do
buildProjectInviteEmail :: System.IO.FilePath
-> ProjectName
-> Email -- Inviting user's email address
-> Email -- Invitee's email address
-> InvitationCode
-> IO Mail
buildProjectInviteEmail templatePath pn fromEmail toEmail invCode = do
template <- maybe (fail "Could not find template for invitation email") pure $
getStringTemplate "invitation_email" templates
let setAttrs = setAttribute "from_email" (from' ^. _Email) .
setAttribute "project_name" pn .
setAttribute "to_email" (to' ^. _Email) .
setAttribute "inv_code" (renderInvCode invCode)
return $ Sendgrid.EmailMessage
{ from = "invitations@aftok.com"
, to = unpack $ to' ^. _Email
, subject = unpack $ "Welcome to the "<>pn<>" Aftok!"
, text = render $ setAttrs template
}
case getStringTemplate "invitation_email" templates of
Nothing -> fail "Could not find template for invitation email"
Just template ->
let setAttrs = setAttribute "from_email" (fromEmail ^. _Email) .
setAttribute "project_name" pn .
setAttribute "to_email" (toEmail ^. _Email) .
setAttribute "inv_code" (renderInvCode invCode)
fromAddr = Address Nothing ("invitations@aftok.com")
toAddr = Address Nothing (toEmail ^. _Email)
subject = "Welcome toEmail the "<>pn<>" Aftok!"
body = plainTextPart . render $ setAttrs template
in pure $ SMTP.simpleMail fromAddr [toAddr] [] [] subject [body]