-- |-- Module: Network.Smtp.Connect-- Copyright: (c) 2010 Ertugrul Soeylemez-- License: BSD3-- Maintainer: Ertugrul Soeylemez <es@ertes.de>-- Stability: experimental---- High level interfaces for networking.{-# LANGUAGE ScopedTypeVariables, TypeFamilies #-}moduleNetwork.Smtp.Connect(-- * ConnectionwithMxConn,withSmtpConn,-- * InitializationwithIsmtp,-- * Utility functionsignoreSigPipe,withIgnoredSigPipe)whereimportControl.ContStuffimportControl.Monad.IO.PeelimportControl.Exception.PeelasEximportNetworkimportNetwork.DnsCacheimportNetwork.Smtp.SimpleimportNetwork.Smtp.TypesimportSystem.IOimportSystem.Posix.Signals-- | Disable the /SIGPIPE/ signal, so our program doesn't die on broken-- pipes.ignoreSigPipe::IO()ignoreSigPipe=()<$installHandlersigPIPEIgnoreNothing-- | Run the given computation with the /SIGPIPE/ signal disabled, so-- our program doesn't die on broken pipes.withIgnoredSigPipe::IOa->IOawithIgnoredSigPipe=Ex.bracket(installHandlersigPIPEIgnoreNothing)(\old->installHandlersigPIPEoldNothing).const-- | Perform some useful (but not necessarily needed) initialization-- like disabling SIGPIPE and initializing sockets, run the given-- computation and then clean up.withIsmtp::IOa->IOawithIsmtp=withSocketsDo.withIgnoredSigPipe-- | Interface to 'withSmtpConn', which connects to the first mail-- exchanger (MX) of the given domain on port 25. The 'Bool' parameter-- specifies whether to fall back to the given domain itself, if no MX-- records can be found.withMxConn::(Applicativem,DnsMonadm,MonadPeelIOm)=>Domain->Bool->MailT(EitherSomeExceptiona)ma->mawithMxConndomainfallbackc=domMx<-resolveMXdomainhostname<-casemMxofJust(mx:_)->returnmx_|fallback->returndomain|otherwise->throwIO$userError"No MX records for domain"withSmtpConnhostname(PortNumber25)c-- | Connect to the specified SMTP server and run the given computation.-- Note that there is also 'withMxConn', which resolves the MX server of-- the given domain.withSmtpConn::forallam.(Applicativem,MonadPeelIOm)=>HostName->PortID->MailT(EitherSomeExceptiona)ma->mawithSmtpConnhostportc=Ex.bracketconnect(liftIO.hClose)$\h->dosendMail_(defSendMailhh)cwhereconnect::mHandleconnect=liftIO$doh<-connectTohostporthSetBufferinghNoBufferinghSetBinaryModehTruereturnh