Skip to content

Commit

Permalink
Merge pull request snoyberg#19 from ibotty/master
Browse files Browse the repository at this point in the history
add convenience functions
  • Loading branch information
snoyberg committed Feb 5, 2014
2 parents 69f2bc9 + f99dfe1 commit 1988e77
Showing 1 changed file with 51 additions and 22 deletions.
73 changes: 51 additions & 22 deletions mime-mail/Network/Mail/Mime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ module Network.Mail.Mime
, renderSendMailCustom
-- * High-level 'Mail' creation
, simpleMail
, simpleMail'
-- * Utilities
, addPart
, addAttachment
, addAttachments
, htmlPart
, plainPart
, randomString
, quotedPrintable
) where

import qualified Data.ByteString.Lazy as L
import Blaze.ByteString.Builder.Char.Utf8
import Blaze.ByteString.Builder
import Blaze.ByteString.Builder.Internal.Write (fromWriteList)
import Data.Monoid
import System.Random
import Control.Arrow
Expand All @@ -35,7 +40,7 @@ import System.IO
import System.Exit
import System.FilePath (takeFileName)
import qualified Data.ByteString.Base64 as Base64
import Control.Monad ((<=<), forM)
import Control.Monad ((<=<), foldM)
import Data.List (intersperse)
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Encoding as LT
Expand Down Expand Up @@ -272,7 +277,7 @@ sendmailCustom :: FilePath -- ^ sendmail executable path
-> L.ByteString -- ^ mail message as lazy bytestring
-> IO ()
sendmailCustom sm opts lbs = do
(Just hin, _, _, phandle) <- createProcess $
(Just hin, _, _, phandle) <- createProcess $
(proc sm opts) { std_in = CreatePipe }
L.hPut hin lbs
hClose hin
Expand Down Expand Up @@ -302,25 +307,49 @@ simpleMail :: Address -- ^ to
-> LT.Text -- ^ HTML body
-> [(Text, FilePath)] -- ^ content type and path of attachments
-> IO Mail
simpleMail to from subject plainBody htmlBody attachments = do
as <- forM attachments $ \(ct, fn) -> do
content <- L.readFile fn
return (ct, fn, content)
return Mail {
mailFrom = from
, mailTo = [to]
, mailCc = []
, mailBcc = []
, mailHeaders = [ ("Subject", subject) ]
, mailParts =
[ Part "text/plain; charset=utf-8" QuotedPrintableText Nothing []
$ LT.encodeUtf8 plainBody
, Part "text/html; charset=utf-8" QuotedPrintableText Nothing []
$ LT.encodeUtf8 htmlBody
] :
(map (\(ct, fn, content) ->
[Part ct Base64 (Just $ T.pack (takeFileName fn)) [] content]) as)
}
simpleMail to from subject plainBody htmlBody attachments =
addAttachments attachments
. addPart [plainPart plainBody, htmlPart htmlBody]
$ mailFromToSubject from to subject

-- | A simple interface for generating an email with only plain-text body.
simpleMail' :: Address -> Address -> Text -> LT.Text -> Mail
simpleMail' to from subject body = addPart [plainPart body]
$ mailFromToSubject from to subject

mailFromToSubject :: Address -> Address -> Text -> Mail
mailFromToSubject to from subject =
(emptyMail from) { mailTo = [to]
, mailHeaders = [("Subject", subject)]
}

-- | Add an 'Alternative' to the 'Mail's parts.
--
-- To e.g. add a plain text body use
-- > addPart [plainPart body] (emptyMail from)
addPart :: Alternatives -> Mail -> Mail
addPart alt mail = mail { mailParts = alt : mailParts mail }

-- | Construct a UTF-8-encoded plain-text 'Part'.
plainPart :: LT.Text -> Part
plainPart body = Part cType QuotedPrintableText Nothing [] $ LT.encodeUtf8 body
where cType = "text/plain; charset=utf-8"

-- | Construct a UTF-8-encoded html 'Part'.
htmlPart :: LT.Text -> Part
htmlPart body = Part cType QuotedPrintableText Nothing [] $ LT.encodeUtf8 body
where cType = "text/html; charset=utf-8"

-- | Add an attachment from a file and construct a 'Part'.
addAttachment :: Text -> FilePath -> Mail -> IO Mail
addAttachment ct fn mail = do
content <- L.readFile fn
let part = Part ct Base64 (Just $ T.pack (takeFileName fn)) [] content
return $ addPart [part] mail

addAttachments :: [(Text, FilePath)] -> Mail -> IO Mail
addAttachments xs mail = foldM fun mail xs
where fun m (c, f) = addAttachment c f m

data QP = QPPlain S.ByteString
| QPNewline
Expand Down

0 comments on commit 1988e77

Please sign in to comment.