local msg = function(mime, s)
  local res = {}
  local a = function(s) table.insert(res, s) end
  a('Content-Type: ')
  a(mime)
  a('; charset="utf-8"\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Disposition: inline\r\n\r\n')
  a(s)
  a('\r\n\r\n')
  return table.concat(res)
end

local plaintext = function(subject, s)
  local res = {}
  local a = function(s) table.insert(res, s) end
  a('Subject: ')
  a(subject)
  a('\r\n')
  a('MIME-Version: 1.0\r\n')
  a(msg('text/plain', s))
  return table.concat(res)
end
local html = function(subject, s)
  local res = {}
  local a = function(s) table.insert(res, s) end
  a('Subject: ')
  a(subject)
  a('\r\n')
  a('MIME-Version: 1.0\r\n')
  a(msg('text/html', s))
  return table.concat(res)
end

local plaintext_html = function(subject, s1, s2)
  local boundary_string = 'wedcoi' -- TODO:check boundary string against s1 and s2
  local res = {}
  local a = function(s) table.insert(res, s) end
  a('Subject: ')
  a(subject)
  a('\r\n')
  a('MIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary="')
  a(boundary_string)
  a('"\r\n\r\n')
  a('--')
  a(boundary_string)
  a('\r\n')
  a(msg('text/plain', s1))
  a('\r\n\r\n')
  a('--')
  a(boundary_string)
  a('\r\n')
  a(msg('text/html', s2))
  a('--')
  a(boundary_string)
  a('--')
  return table.concat(res)
end

return {
  plaintext=plaintext,
  html=html,
  plaintext_html=plaintext_html
  }
