|  | email.Message problem |  | |
| | | Corrado Gioannini |  |
| Posted: Tue Sep 02, 2008 2:04 pm Post subject: email.Message problem |  |
| |  | |
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@mydomain' messg['From'] = 'email.y@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) |
| |
| | | Gabriel Genellina |  |
| Posted: Tue Sep 02, 2008 5:50 pm Post subject: Re: email.Message problem |  |
En Tue, 02 Sep 2008 13:04:18 -0300, Corrado Gioannini <gioco@nekhem.com> escribió:
| Quote: | 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
messg = email.message.Message()
|
Replace this line with: messg = email.mime.multipart.MIMEMultipart() *OR* Set the Content-Type header to "multipart/mixed" *before* anything else.
-- Gabriel Genellina |
| |
| | | Corrado Gioannini |  |
| Posted: Wed Sep 03, 2008 8:36 am Post subject: Re: email.Message problem |  |
On Tue, Sep 02, 2008 at 04:50:15PM -0300, Gabriel Genellina wrote:
| Quote: | messg = email.message.Message()
Replace this line with: messg = email.mime.multipart.MIMEMultipart() *OR* Set the Content-Type header to "multipart/mixed" *before* anything else.
|
thanks Gabriel!
i coudn't have thought that setting the header in a different order could affect the as_string method. i'll take a look at the code. :)
c. -- no, sono sempre io, non mi cambierete quel che ho dentro forse ho solo un'altra faccia ho più cicatrici di prima, sorrido un po' meno, forse penso di più. (Kina) |
| |
| | | Gabriel Genellina |  |
| Posted: Thu Sep 04, 2008 1:11 am Post subject: Re: email.Message problem |  |
| |  | |
En Wed, 03 Sep 2008 07:36:30 -0300, Corrado Gioannini <gioco@nekhem.com> escribi�:
| Quote: | On Tue, Sep 02, 2008 at 04:50:15PM -0300, Gabriel Genellina wrote: messg = email.message.Message()
Replace this line with: messg = email.mime.multipart.MIMEMultipart() *OR* Set the Content-Type header to "multipart/mixed" *before* anything else.
thanks Gabriel!
i coudn't have thought that setting the header in a different order could affect the as_string method. i'll take a look at the code. 
|
Yes. This is the offending sequence:
messg = email.message.Message() messg.set_charset('ISO-8859-15') messg["Content-type"] = "Multipart/mixed"
"Charset" is not a header by itself; it is an optional parameter of the Content-Type header. When you call set_charset(), a default 'Content-Type: text/plain; charset="ISO-8859-15"' header is added. Later, you *add* a new Content-type header -- remember that the [] notation *appends* a new header instead of replacing an existing one (yes, it is somewhat confusing...) Your message ends up being text/plain, not multiplart as intended. It's better to be explicit and use the email.mime.multipart.MIMEMultipart class.
-- Gabriel Genellina |
| |
|
|