diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 06236c3e1..1ad7b19e8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -99,6 +99,8 @@ The documentation reads a bit like a tutorial, but should still be kept somewhat - Please add Haddock-comments to new methods intended to be used by directly when developing using IHP. +- Please consider carefully before adding new packages as requirements to IHP itself. Make sure the packages are actively maintained. + ## Running Tests When inside the IHP directory, you can run the Test Suite by loading it into a `ghci` like this: diff --git a/IHP/Mail.hs b/IHP/Mail.hs index aa6f3d3d7..621692842 100644 --- a/IHP/Mail.hs +++ b/IHP/Mail.hs @@ -19,10 +19,19 @@ import IHP.FrameworkConfig import Network.Mail.Mime import qualified Network.Mail.Mime.SES as Mailer +import qualified Network.Mail.SMTP as SMTP import qualified Network.HTTP.Client import qualified Network.HTTP.Client.TLS import Text.Blaze.Html5 (Html) import qualified Text.Blaze.Html.Renderer.Text as Blaze +import qualified Data.ByteString.Lazy.Char8 as B8 +import qualified Data.Text as T +import Data.Maybe + +import Control.Exception +data MyException = ThisException | ThatException + deriving (Show, Typeable) +instance Exception MyException buildMail :: (BuildMail mail, ?context :: context, ConfigProvider context) => mail -> IO Mail buildMail mail = let ?mail = mail in simpleMail (to mail) from subject (cs $ text mail) (html mail |> Blaze.renderHtml) [] @@ -46,6 +55,16 @@ sendWithMailServer SES { .. } mail = do } Mailer.renderSendMailSES manager ses mail +sendWithMailServer SendGrid { .. } mail = do + let mail' = if isJust category then mail {mailHeaders = ("X-SMTPAPI","{\"category\": \"" ++ (fromJust category) ++ "\"}") : headers} else mail + SMTP.sendMailWithLoginSTARTTLS' "smtp.sendgrid.net" 587 "apikey" (T.unpack apiKey) mail' + where headers = mailHeaders mail + +sendWithMailServer GenericSMTP { .. } mail + | isNothing credentials = SMTP.sendMail' host port mail + | otherwise = SMTP.sendMailWithLogin' host port (fst creds) (snd creds) mail + where creds = fromJust credentials + sendWithMailServer Sendmail mail = do message <- renderMail' mail sendmail message @@ -68,4 +87,4 @@ class BuildMail mail where -- | When no plain text version of the email is specified it falls back to using the html version but striping out all the html tags text :: (?context :: context, ConfigProvider context) => mail -> Text - text mail = stripTags (cs $ Blaze.renderHtml (html mail)) \ No newline at end of file + text mail = stripTags (cs $ Blaze.renderHtml (html mail)) diff --git a/IHP/Mail/Types.hs b/IHP/Mail/Types.hs index 2c9168955..bc5fcffb2 100644 --- a/IHP/Mail/Types.hs +++ b/IHP/Mail/Types.hs @@ -9,17 +9,22 @@ module IHP.Mail.Types where import IHP.Prelude -import Network.Mail.Mime -import qualified Network.Mail.Mime.SES as Mailer -import Text.Blaze.Html5 (Html) -import qualified Text.Blaze.Html.Renderer.Text as Blaze +import Network.Socket (PortNumber) -- | Configuration for a mailer used by IHP data MailServer = - -- | Uses AWS SES for sending emails. Highly recommended in production + -- | Uses AWS SES for sending emails SES { accessKey :: ByteString , secretKey :: ByteString -- | E.g. @"us-east-1"@ or @"eu-west-1"@ , region :: Text } - -- | Uses the local Sendmail binary for sending emails + -- | Uses the local Sendmail binary for sending emails. Avoid this with IHP Cloud | Sendmail + -- | Uses SendGrid for sending emails + | SendGrid { apiKey :: Text + , category :: Maybe Text } + -- | Uses a generic SMTP for sending emails + | GenericSMTP { host :: String + , port :: PortNumber + -- (Username,Password) combination + , credentials :: Maybe (String, String)} diff --git a/ihp.cabal b/ihp.cabal index e336006e5..838536f72 100644 --- a/ihp.cabal +++ b/ihp.cabal @@ -73,6 +73,7 @@ common shared-properties , binary , mime-mail , mime-mail-ses + , smtp-mail , http-client , http-client-tls , resource-pool diff --git a/ihp.nix b/ihp.nix index f7599d273..5fbe6524b 100644 --- a/ihp.nix +++ b/ihp.nix @@ -34,6 +34,7 @@ , wai-websockets , mime-mail , mime-mail-ses +, smtp-mail , attoparsec , case-insensitive , http-media @@ -89,6 +90,7 @@ mkDerivation { wai-websockets mime-mail mime-mail-ses + smtp-mail attoparsec case-insensitive http-media diff --git a/shell.nix b/shell.nix index 2e978c2b0..4513037cf 100644 --- a/shell.nix +++ b/shell.nix @@ -35,6 +35,7 @@ let wai-websockets mime-mail mime-mail-ses + smtp-mail attoparsec case-insensitive http-media