renderMail
  :: Address    -- ^ from
  -> [Address]  -- ^ to
  -> [Address]  -- ^ cc
  -> [Address]  -- ^ bcc
  -> S.Text     -- ^ subject
  -> L.Text     -- ^ body
  -> [FilePath] -- ^ attachments
  -> IO L.ByteString
renderMail from to cc bcc subject body attach = do
  parts <- forM attach $ \ path -> do
    content <- readFile path
    let mime = getMime $ takeExtension path
        file = Just . S.pack $ takeFileName path
    return $! [Part mime Base64 file [] content]
  let plain = [Part "text/plain; charset=utf-8" QuotedPrintableText Nothing [] $ encodeUtf8 body]
  mail <- renderMail' . Mail from to cc bcc headers $ plain : parts
  return $! replace "\n." ("\n.." :: L.ByteString) mail <> "\r\n.\r\n"
  where headers = [("Subject", subject)]

-- |
-- Get the mime type for the given file extension.
renderMail
  :: Address    -- ^ from
  -> [Address]  -- ^ to
  -> [Address]  -- ^ cc
  -> [Address]  -- ^ bcc
  -> S.Text     -- ^ subject
  -> L.Text     -- ^ body
  -> [FilePath] -- ^ attachments
  -> IO L.ByteString
renderMail from to cc bcc subject body attach = do
  parts <- forM attach $ \ path -> do
    content <- readFile path
    let mime = getMime $ takeExtension path
        file = Just . S.pack $ takeFileName path
    return $! [Part mime Base64 file [] content]
  let plain = [Part "text/plain; charset=utf-8" QuotedPrintableText Nothing [] $ encodeUtf8 body]
  mail <- renderMail' . Mail from to cc bcc headers $ plain : parts
  return $! replace "\n." ("\n.." :: L.ByteString) mail <> "\r\n.\r\n"
  where headers = [("Subject", subject)]

-- |
-- Get the mime type for the given file extension.
renderMail from to cc bcc subject body attach = do
  parts <- forM attach $ \ path -> do
    content <- readFile path
    let mime = getMime $ takeExtension path
        file = Just . S.pack $ takeFileName path
    return $! [Part mime Base64 file [] content]
  let plain = [Part "text/plain; charset=utf-8" QuotedPrintableText Nothing [] $ encodeUtf8 body]
  mail <- renderMail' . Mail from to cc bcc headers $ plain : parts
  return $! replace "\n." ("\n.." :: L.ByteString) mail <> "\r\n.\r\n"
  where headers = [("Subject", subject)]

-- |
-- Get the mime type for the given file extension.