email.Message problem

Corrado Gioannini gioco at nekhem.com
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:

mailtest.py
----------------------------------------------------------------------
# coding: ISO-8859-15

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

messg = email.message.Message()
messg.set_charset('ISO-8859-15')

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')
messg.attach(pl)

messg.as_string()
----------------------------------------------------------------------

running it gives:

Traceback (most recent call last):
  File "mailtest.py", line 22, in <module>
    messg.as_string()
  File "/usr/lib/python2.5/email/message.py", line 131, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib/python2.5/email/generator.py", line 84, in flatten
    self._write(msg)
  File "/usr/lib/python2.5/email/generator.py", line 109, in _write
    self._dispatch(msg)
  File "/usr/lib/python2.5/email/generator.py", line 135, in _dispatch
    meth(msg)
  File "/usr/lib/python2.5/email/generator.py", line 175, in
_handle_text
    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 :-)

10x,
C.
-- 
Quando l'infanzia muore i suoi cadaveri sono chiamati adulti.
                                                 (B. Aldiss)




More information about the Python-list mailing list