def findPayload(message, type):
  """
  Find a payload/part that matches a type as closely as possible and decode it
  properly.

  Parameters
  ----------
  message : email.message.Message
    The message to search.
  type : str
    A MIME type.

  Returns
  -------
  str
    The payload as a string.
  """
  charset = message.get_charset() or "utf-8"
  if message.is_multipart():
    for k in message.walk():
      contenttype = k.get_content_type()
      if contenttype == type:
        return k.get_payload(decode=True).decode(charset), contenttype
    for k in message.walk():
      contenttype = k.get_content_type()
      if k.get_content_type() == message.get_default_type():
        return k.get_payload(decode=True).decode(charset), contenttype
  return message.get_payload(decode=True).decode(charset), message.get_content_type()