email.Message problem

Corrado Gioannini gioco at
Tue Sep 2 18:04:18 CEST 2008

Hi all,
i know i'm probably going to ask a very silly question, but i
can't figure out where i'm doing wrong just reading the docs.

trying to build and send a mail message using the email.* modules
(with python 2.5).

a simplified version of my script that breaks is this:
# coding: ISO-8859-15

import os, mimetypes, smtplib, base64, StringIO
import email.message, email.header, email.generator

messg = email.message.Message()

messg['To'] = 'email.x at mydomain'
messg['From'] = 'email.y at mydomain'
messg['Subject'] = email.header.Header("This is a test.", 'ISO-8859-15')
messg["Message-ID"] = email.Utils.make_msgid()
messg["Content-type"] = "Multipart/mixed"
messg.preamble = "Mime test\n"

pl = email.Message.Message()
pl.add_header("Content-type", "text/plain; charset=ISO-8859-15")
pl.add_header("Content-transfer-encoding", "8bit")
pl.set_payload("Body text goes here.\n", 'ISO-8859-15')


running it gives:

Traceback (most recent call last):
  File "", line 22, in <module>
  File "/usr/lib/python2.5/email/", line 131, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib/python2.5/email/", line 84, in flatten
  File "/usr/lib/python2.5/email/", line 109, in _write
  File "/usr/lib/python2.5/email/", line 135, in _dispatch
  File "/usr/lib/python2.5/email/", line 175, in
    raise TypeError('string payload expected: %s' % type(payload))
TypeError: string payload expected: <type 'list'>

the real case is just a little more complex: i have to attach some
files and then send the message with smtplib to a list of recipients.

no problems when attaching files etc., but when i try to get the
message value 'flattened' to pass it to smtplib i always get an error
as above.

it seems to work only if i don't use .attach but just .set_payload in
the main message, but in this case i cannot attach the extra files
(or maybe i'm wrong in this?)

any help (or pointers to docs/examples etc) are very appreciated :-)

Quando l'infanzia muore i suoi cadaveri sono chiamati adulti.
                                                 (B. Aldiss)

More information about the Python-list mailing list