3.2: email.message.get_payload() delivers str, but send_message expect bytes

Axel Rau axel.nospam.rau at Chaos1.DE
Fri Apr 8 13:43:53 CEST 2011

Hi all,

I'm just starting with imaplib, email and smtplib and try to write a
SPAM reporter. I retrieve SPAM mails from an IMAP server and add them as
message/rfc822 attachments to a report mail.
Sometimes my call of smtplib.send_message works, sometimes, I get:
line 771, in send_message
line 739, in sendmail
    (code,resp) = self.data(msg)
line 495, in data
    q = _quote_periods(msg)
line 165, in _quote_periods
    return re.sub(br'(?m)^\.', '..', bindata)
line 167, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: sequence item 1: expected bytes, str found
When I query the class of my pyloads, they always show up as strings.
The test case, which always fails is an oversized SPAM, which my script
must truncate. I do this by removing MIME parts from the end (just
deleting items from the list, describing the multipart structure).

Another problem comes up, when I try to encode the payload of the whole
report mail, I get always:
  File "erdb_bt.py", line 195, in flushReports
line 32, in encode_base64
    encdata = str(_bencode(orig), 'ascii')
line 56, in b64encode
    raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not list
What am I doing wrong?


More information about the Python-list mailing list