Skip to content

Commit

Permalink
Prepare v0.5.0, polish, filePart interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Apr 6, 2019
1 parent ba627d0 commit 6d97be4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
8 changes: 8 additions & 0 deletions mime-mail/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.5

* Add support for inline images and multipart/related
* Add `filePart` and `filePartBS` for attachments
* Change `Part` data type (`Disposition` and `PartContent`)
* Remove `addAttachmentCid` and `addAttachmentBSCid` (replaced by `addImage`)
* Remove `getAttachmentPartBS` and `getAttachmentPart`

## 0.4.14

* Add `IsString` instance for `Address`
Expand Down
45 changes: 31 additions & 14 deletions mime-mail/Network/Mail/Mime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module Network.Mail.Mime
, Alternatives
, Part (..)
, PartContent (..)
, Disposition (..)
, Encoding (..)
, InlineImage(..)
, ImageContent(..)
, Headers
-- * Render a message
, renderMail
Expand All @@ -33,10 +36,10 @@ module Network.Mail.Mime
, renderAddress
, htmlPart
, plainPart
, filePart
, filePartBS
, randomString
, quotedPrintable
, InlineImage(..)
, ImageContent(..)
, relatedPart
, addImage
, mkImageParts
Expand Down Expand Up @@ -475,12 +478,6 @@ simpleMailInMemory to from subject plainBody htmlBody attachments =
. addPart [plainPart plainBody, htmlPart htmlBody]
$ mailFromToSubject from to subject

-- | An interface for generating an email with HTML and plain-text
-- alternatives, some file attachments, and inline images.
-- Note that we use lazy IO for reading in the attachment and inlined images.
-- Inline images can be referred to from the HTML content using
-- the @src="cid:{{CONTENT-ID}}"@ syntax, where CONTENT-ID is
-- the filename of the image.

data InlineImage = InlineImage {
imageContentType :: Text
Expand All @@ -491,6 +488,14 @@ data InlineImage = InlineImage {
data ImageContent = ImageFilePath FilePath | ImageByteString L.ByteString
deriving Show

-- | An interface for generating an email with HTML and plain-text
-- alternatives, some file attachments, and inline images.
-- Note that we use lazy IO for reading in the attachment and inlined images.
-- Inline images can be referred to from the HTML content using
-- the @src="cid:{{CONTENT-ID}}"@ syntax, where CONTENT-ID is
-- the filename of the image.
--
-- Since 0.5.0
simpleMailWithImages :: [Address] -- ^ to (multiple)
-> Address -- ^ from
-> Text -- ^ subject
Expand Down Expand Up @@ -539,19 +544,33 @@ htmlPart body = Part cType QuotedPrintableText DefaultDisposition []
$ PartContent (LT.encodeUtf8 body)
where cType = "text/html; charset=utf-8"

-- | Construct a BASE64-encoded file attachment 'Part'
--
-- Since 0.5.0
filePart :: Text -> FilePath -> IO Part
filePart ct fn = do
content <- L.readFile fn
return $ filePartBS ct (T.pack (takeFileName fn)) content

-- | Construct a BASE64-encoded file attachment 'Part'
--
-- Since 0.5.0
filePartBS :: Text -> Text -> L.ByteString -> Part
filePartBS ct filename content = Part ct Base64 (AttachmentDisposition filename) [] (PartContent content)

-- | 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 (AttachmentDisposition $ T.pack (takeFileName fn)) []
(PartContent content)
part <- filePart ct fn
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

-- | Add an inline image from a file and construct a 'Part'.
--
-- Since 0.5.0
addImage :: InlineImage -> IO Part
addImage InlineImage{..} = do
content <- case imageContent of
Expand All @@ -571,9 +590,7 @@ addAttachmentBS :: Text -- ^ content type
-> Text -- ^ file name
-> L.ByteString -- ^ content
-> Mail -> Mail
addAttachmentBS ct fn content mail =
let part = Part ct Base64 (AttachmentDisposition fn) [] (PartContent content)
in addPart [part] mail
addAttachmentBS ct fn content mail = addPart [filePartBS ct fn content] mail

-- |
-- Since 0.4.7
Expand Down
4 changes: 2 additions & 2 deletions mime-mail/mime-mail.cabal
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Name: mime-mail
Version: 0.4.14
Version: 0.5.0
Synopsis: Compose MIME email messages.
description: Hackage documentation generation is not reliable. For up to date documentation, please see: <http://www.stackage.org/package/mime-mail>.
Homepage: http://github.com/snoyberg/mime-mail
License: MIT
License-file: LICENSE
Author: Michael Snoyman <[email protected]>
Maintainer: Michael Snoyman <[email protected]>
Maintainer: Michael Snoyman <[email protected]>, Marek Suchánek <[email protected]>
Category: Email
Build-type: Simple
extra-source-files: README.md
Expand Down

0 comments on commit 6d97be4

Please sign in to comment.