smtp-mail 0.1.4.6 → 0.2.0.0
raw patch · 5 files changed
+147/−36 lines, 5 filesdep +connectiondep +network-bsddep ~basedep ~bytestringdep ~cryptohashPVP ok
version bump matches the API change (PVP)
Dependencies added: connection, network-bsd
Dependency ranges changed: base, bytestring, cryptohash, mime-mail, network
API changes (from Hackage documentation)
- Network.Mail.SMTP.Types: data Address :: *
+ Network.Mail.SMTP.Types: data Address
Files
- CHANGELOG.md +31/−0
- Network/Mail/SMTP.hs +38/−30
- Network/Mail/SMTP/Auth.hs +0/−1
- README.md +70/−0
- smtp-mail.cabal +8/−5
+ CHANGELOG.md view
@@ -0,0 +1,31 @@+# CHANGELOG++This package follows the Package Versioning Policy.+Roughly speaking, this means that we have four digits standing for:++- Major: A significant rewrite of the library.+- Major: A breaking change+- Minor: A non-breaking addition+- Patch: A non-breaking bugfix++## Upcoming++If you are doing a pull-request, please update this list.+A template is provided:++```+- [# PR number](URL to pr) @your_github_username+ - Describe change #1+ - Describe change #2+ - Indicate if changes are major, minor, or patch changes.+```++## 0.2.0.0++- [#25](https://github.com/jhickner/smtp-mail/pull/25) @shulhi+ - References to the deprecated `Network` module were removed and replaced + with the new `connection` package. + - Duplicate functionality was deprecated.+- [#23](https://github.com/jhickner/smtp-mail/pull/23) @alexandersgreen+ - The `Cc` and `Bcc` fields will be sent to the SMTP server, and they'll + actually be sent now.
Network/Mail/SMTP.hs view
@@ -40,16 +40,15 @@ import Network.Mail.SMTP.Auth import Network.Mail.SMTP.Types -import System.IO import System.FilePath (takeFileName) import Control.Monad (unless)-import Data.Monoid import Data.Char (isDigit) -import Network+import Network.Socket import Network.BSD (getHostName)-import Network.Mail.Mime hiding (htmlPart, simpleMail)+import Network.Mail.Mime hiding (filePart, htmlPart, simpleMail)+import qualified Network.Connection as Conn import Data.ByteString (ByteString) import qualified Data.ByteString as B@@ -60,10 +59,10 @@ import qualified Data.Text.Lazy.Encoding as TL import Data.Text.Encoding -data SMTPConnection = SMTPC !Handle ![ByteString]+data SMTPConnection = SMTPC !Conn.Connection ![ByteString] instance Eq SMTPConnection where- (==) (SMTPC a _) (SMTPC b _) = a == b+ (==) (SMTPC a _) (SMTPC b _) = Conn.connectionID a == Conn.connectionID b -- | Connect to an SMTP server with the specified host and default port (25) connectSMTP :: HostName -- ^ name of the server@@ -74,8 +73,11 @@ connectSMTP' :: HostName -- ^ name of the server -> PortNumber -- ^ port number -> IO SMTPConnection-connectSMTP' hostname port =- connectTo hostname (PortNumber port) >>= connectStream getHostName+connectSMTP' hostname port = do+ context <- Conn.initConnectionContext+ Conn.connectTo context connParams >>= connectStream getHostName+ where+ connParams = Conn.ConnectionParams hostname port Nothing Nothing -- | Connect to an SMTP server with the specified host and port@@ -83,8 +85,11 @@ -> PortNumber -- ^ port number -> IO String -- ^ Returns the host name to use to send from -> IO SMTPConnection-connectSMTPWithHostName hostname port getMailHostName =- connectTo hostname (PortNumber port) >>= connectStream getMailHostName+connectSMTPWithHostName hostname port getMailHostName = do+ context <- Conn.initConnectionContext+ Conn.connectTo context connParams >>= connectStream getMailHostName+ where+ connParams = Conn.ConnectionParams hostname port Nothing Nothing -- | Attemp to send a 'Command' to the SMTP server once@@ -115,11 +120,11 @@ else return (code, msg) -- | Create an 'SMTPConnection' from an already connected Handle-connectStream :: IO String -> Handle -> IO SMTPConnection+connectStream :: IO String -> Conn.Connection -> IO SMTPConnection connectStream getMailHostName st = do (code1, _) <- parseResponse st unless (code1 == 220) $ do- hClose st+ Conn.connectionClose st fail "cannot connect to the server" senderHost <- getMailHostName (code, initialMsg) <- tryCommandNoFail 3 (SMTPC st []) (EHLO $ B8.pack senderHost) 250@@ -129,18 +134,18 @@ msg <- tryCommand 3 (SMTPC st []) (HELO $ B8.pack senderHost) 250 return (SMTPC st (tail $ B8.lines msg)) -parseResponse :: Handle -> IO (ReplyCode, ByteString)-parseResponse st = do+parseResponse :: Conn.Connection -> IO (ReplyCode, ByteString)+parseResponse conn = do (code, bdy) <- readLines return (read $ B8.unpack code, B8.unlines bdy) where readLines = do- l <- B8.hGetLine st- let (c, bdy) = B8.span isDigit l- if not (B8.null bdy) && B8.head bdy == '-'- then do (c2, ls) <- readLines- return (c2, B8.tail bdy:ls)- else return (c, [B8.tail bdy])+ l <- Conn.connectionGetLine 1000 conn+ let (c, bdy) = B8.span isDigit l+ if not (B8.null bdy) && B8.head bdy == '-'+ then do (c2, ls) <- readLines+ return (c2, B8.tail bdy:ls)+ else return (c, [B8.tail bdy]) -- | Send a 'Command' to the SMTP server@@ -210,7 +215,7 @@ -- | Send 'QUIT' and close the connection. closeSMTP :: SMTPConnection -> IO ()-closeSMTP c@(SMTPC conn _) = sendCommand c QUIT >> hClose conn+closeSMTP c@(SMTPC conn _) = sendCommand c QUIT >> Conn.connectionClose conn -- | Sends a rendered mail to the server. sendRenderedMail :: ByteString -- ^ sender mail@@ -232,7 +237,7 @@ sendRenderedMail from to rendered conn where enc = encodeUtf8 . addressEmail from = enc mailFrom- to = map enc mailTo+ to = map enc $ mailTo ++ mailCc ++ mailBcc -- | Connect to an SMTP server, send a 'Mail', then disconnect. Uses the default port (25). sendMail :: HostName -> Mail -> IO ()@@ -283,7 +288,7 @@ rendered <- BL.toStrict `fmap` renderMail' mail sendRenderedMail sender to rendered conn where enc = encodeUtf8 . addressEmail- to = map enc mailTo+ to = map enc $ mailTo ++ mailCc ++ mailBcc -- | A convenience function that sends 'AUTH' 'LOGIN' to the server login :: SMTPConnection -> UserName -> Password -> IO (ReplyCode, ByteString)@@ -309,13 +314,15 @@ -- | Construct a plain text 'Part' plainTextPart :: TL.Text -> Part-plainTextPart = Part "text/plain; charset=utf-8"- QuotedPrintableText Nothing [] . TL.encodeUtf8+plainTextPart body = Part "text/plain; charset=utf-8"+ QuotedPrintableText DefaultDisposition [] (PartContent $ TL.encodeUtf8 body)+{-# DEPRECATED plainTextPart "Use plainPart from mime-mail package" #-} -- | Construct an html 'Part' htmlPart :: TL.Text -> Part-htmlPart = Part "text/html; charset=utf-8"- QuotedPrintableText Nothing [] . TL.encodeUtf8+htmlPart body = Part "text/html; charset=utf-8"+ QuotedPrintableText DefaultDisposition [] (PartContent $ TL.encodeUtf8 body)+{-# DEPRECATED htmlPart "Use htmlPart from mime-mail package" #-} -- | Construct a file attachment 'Part' filePart :: T.Text -- ^ content type@@ -323,7 +330,8 @@ -> IO Part filePart ct fp = do content <- BL.readFile fp- return $ Part ct Base64 (Just $ T.pack (takeFileName fp)) [] content+ return $ Part ct Base64 (AttachmentDisposition $ T.pack (takeFileName fp)) [] (PartContent content)+{-# DEPRECATED filePart "Use filePart from mime-mail package" #-} lazyToStrict :: BL.ByteString -> B.ByteString lazyToStrict = B.concat . BL.toChunks@@ -331,5 +339,5 @@ crlf :: B8.ByteString crlf = B8.pack "\r\n" -bsPutCrLf :: Handle -> ByteString -> IO ()-bsPutCrLf h s = B8.hPut h s >> B8.hPut h crlf >> hFlush h+bsPutCrLf :: Conn.Connection -> ByteString -> IO ()+bsPutCrLf conn = Conn.connectionPut conn . flip B.append crlf
Network/Mail/SMTP/Auth.hs view
@@ -13,7 +13,6 @@ import Data.ByteString (ByteString) import Data.List import Data.Bits-import Data.Monoid import qualified Data.ByteString as B import qualified Data.ByteString.Char8 as B8 (unwords)
+ README.md view
@@ -0,0 +1,70 @@+SMTP-MAIL+=========++Making it easy to send SMTP emails from Haskell.++```+cabal install smtp-mail+```++### Sending with an SMTP server++```haskell+{-# LANGUAGE OverloadedStrings #-}++import Network.Mail.SMTP++from = Address Nothing "email@domain.com"+to = [Address (Just "Jason Hickner") "email@domain.com"]+cc = []+bcc = []+subject = "email subject"+body = plainTextPart "email body"+html = htmlPart "<h1>HTML</h1>"++mail = simpleMail from to cc bcc subject [body, html]++main = sendMail host mail+```++or with an attachment:++```haskell+main = do+ attachment <- filePart "application/octet-stream" "path/to/attachment.zip"+ let mail = simpleMail from to cc bcc subject [body, html, attachment]+ sendMail host mail+```++or, with authentication:++```haskell+main = sendMailWithLogin host user pass mail+```++Note: `sendMail'` and `sendMailWithLogin'` variations are also provided if you want to specify a port as well as a hostname.+++### Sending with sendmail++If you'd like to use sendmail, the sendmail interface from ```Network.Mail.Mime``` +is reexported as well:++```haskell+-- send via the default sendmail executable with default options+renderSendMail mail++-- send via the specified executable with specified options+renderSendMailCustom filepath [opts] mail+```++For more complicated scenarios or for adding attachments or CC/BCC+addresses you can import ```Network.Mail.Mime``` and construct ```Mail```+objects manually.+++### Thanks++This library is based on code from HaskellNet, which appears to be no longer+maintained. I've cleaned up the error handling, added some API functions to+make common operations easier, and switched to ByteStrings where applicable.
smtp-mail.cabal view
@@ -1,10 +1,7 @@--- Initial smtp-mail.cabal generated by cabal init. For further--- documentation, see http://haskell.org/cabal/users-guide/- name: smtp-mail-version: 0.1.4.6+version: 0.2.0.0 synopsis: Simple email sending via SMTP--- description:+description: This packages provides a simple interface for mail over SMTP. PLease see the README for more information. homepage: http://github.com/jhickner/smtp-mail license: BSD3 license-file: LICENSE@@ -15,6 +12,10 @@ build-type: Simple cabal-version: >=1.8 +extra-source-files:+ README.md+ , CHANGELOG.md+ source-repository head type: git location: git@github.com:jhickner/smtp-mail.git@@ -32,10 +33,12 @@ , base16-bytestring , base64-bytestring , bytestring+ , connection , cryptohash , filepath , mime-mail , network+ , network-bsd , text ghc-options: -Wall -fwarn-tabs