When content takes more bytes than chars, the following command wrongly encodes text:
content = tools.to_bytes(
u"{%d+}%s%s" % (len(content), str(CRLF), content))
This code works correctly:
content = tools.to_bytes(content)
len1 = tools.to_bytes(u"{%d+}" % len(content))
content = len1 + CRLF + content