Skip to content

Commit

Permalink
added support for SendGrid and Generic SMTP
Browse files Browse the repository at this point in the history
  • Loading branch information
fegu committed Dec 23, 2020
1 parent 5ffac97 commit bd33f99
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 20 additions & 1 deletion IHP/Mail.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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) []
Expand All @@ -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
Expand All @@ -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))
text mail = stripTags (cs $ Blaze.renderHtml (html mail))
17 changes: 11 additions & 6 deletions IHP/Mail/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
1 change: 1 addition & 0 deletions ihp.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ common shared-properties
, binary
, mime-mail
, mime-mail-ses
, smtp-mail
, http-client
, http-client-tls
, resource-pool
Expand Down
2 changes: 2 additions & 0 deletions ihp.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
, wai-websockets
, mime-mail
, mime-mail-ses
, smtp-mail
, attoparsec
, case-insensitive
, http-media
Expand Down Expand Up @@ -89,6 +90,7 @@ mkDerivation {
wai-websockets
mime-mail
mime-mail-ses
smtp-mail
attoparsec
case-insensitive
http-media
Expand Down
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ let
wai-websockets
mime-mail
mime-mail-ses
smtp-mail
attoparsec
case-insensitive
http-media
Expand Down

0 comments on commit bd33f99

Please sign in to comment.