module Network.Smtp.Connect
(
withMxConn,
withSmtpConn,
withIsmtp
)
where
import Control.ContStuff
import Control.Monad.IO.Peel
import Control.Exception.Peel as Ex
import Network
import Network.DnsCache
import Network.Smtp.Simple
import Network.Smtp.Types
import System.IO
withIsmtp :: IO a -> IO a
withIsmtp = withSocketsDo
withMxConn ::
(Applicative m, DnsMonad m, MonadPeelIO m) =>
Domain -> Bool -> MailT a m a -> m a
withMxConn domain fallback c = do
mMx <- resolveMX domain
hostname <-
case mMx of
Just (mx:_) -> return mx
_ | fallback -> return domain
| otherwise -> throwIO $ userError "No MX records for domain"
withSmtpConn hostname (PortNumber 25) c
withSmtpConn ::
forall a m. (Applicative m, MonadPeelIO m) =>
HostName -> PortID -> MailT a m a -> m a
withSmtpConn host port c =
Ex.bracket connect (liftIO . hClose) $ \h -> do
sendMail_ (defSendMail h h) c
where
connect :: m Handle
connect =
liftIO $ do
h <- connectTo host port
hSetBuffering h NoBuffering
hSetBinaryMode h True
return h