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

Axel Rau axel.nospam.rau at Chaos1.DE
Fri Apr 8 07:43:53 EDT 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:
----------
  File
"/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/smtplib.py",
line 771, in send_message
    rcpt_options)
  File
"/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/smtplib.py",
line 739, in sendmail
    (code,resp) = self.data(msg)
  File
"/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/smtplib.py",
line 495, in data
    q = _quote_periods(msg)
  File
"/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/smtplib.py",
line 165, in _quote_periods
    return re.sub(br'(?m)^\.', '..', bindata)
  File
"/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/re.py",
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
    email.encoders.encode_base64(self.msg)
  File
"/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/email/encoders.py",
line 32, in encode_base64
    encdata = str(_bencode(orig), 'ascii')
  File
"/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/base64.py",
line 56, in b64encode
    raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not list
-------
What am I doing wrong?

Axel



More information about the Python-list mailing list