Strange effects using the email.message.Message class

Discussion in 'Python' started by Rogério Brito, Sep 9, 2009.

  1. Dear people,

    I am a newbie in Python and I'm just writing a program to rip the HTML parts of
    multipart/alternative e-mails that come with a plain text version.

    For this task, I decided to leave perl aside for this task and tried to code
    something in Python using what is already available in the libraries.

    Unfortunately, the program that I coded exhibits a strange problem, even if I
    follow what is written on the reference for Python 2.6 (I'm actually running
    Debian GNU/Linux on my system, sid, updated daily).

    The strange thing is that I even checked page 500 of "Python Cookbook", by Alex
    Martelli, et. al. and the code in that page is actually quite similar to what I
    wrote.

    I'm attaching the program that I've written here, together with a fake e-mail
    that triggers the bug.

    The messages that I get, BTW, are the following:

    # http://www.python.org/doc/2.5.2/lib/module-email.message.html
    # http://docs.python.org/library/email.message.html
    #
    # Traceback (most recent call last):
    # File "test.py", line 8, in <module>
    # print msg.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'>

    If anybody could help me with the code, I would be very grateful. Also, if you
    can criticize what I wrote, I would be very happy, as I would like to improve my
    Python-fu (which is minimal, unfortunately).


    Thanks in advance, Rogério Brito.

    --
    Rogério Brito : rbrito@{mackenzie,ime.usp}.br : GPG key 1024D/7C2CAEB8
    http://www.ime.usp.br/~rbrito : http://meusite.mackenzie.com.br/rbrito
    Projects: algorithms.berlios.de : lame.sf.net : vrms.alioth.debian.org
    Rogério Brito, Sep 9, 2009
    #1
    1. Advertising

  2. Rogério Brito

    Sean DiZazzo Guest

    On Sep 8, 6:23 pm, Rogério Brito <> wrote:
    > Dear people,
    >
    > I am a newbie in Python and I'm just writing a program to rip the HTML parts of
    > multipart/alternative e-mails that come with a plain text version.
    >
    > For this task, I decided to leave perl aside for this task and tried to code
    > something in Python using what is already available in the libraries.
    >
    > Unfortunately, the program that I coded exhibits a strange problem, even if I
    > follow what is written on the reference for Python 2.6 (I'm actually running
    > Debian GNU/Linux on my system, sid, updated daily).
    >
    > The strange thing is that I even checked page 500 of "Python Cookbook", by Alex
    > Martelli, et. al. and the code in that page is actually quite similar to what I
    > wrote.
    >
    > I'm attaching the program that I've written here, together with a fake e-mail
    > that triggers the bug.
    >
    > The messages that I get, BTW, are the following:
    >
    > #http://www.python.org/doc/2.5.2/lib/module-email.message.html
    > #http://docs.python.org/library/email.message.html
    > #
    > # Traceback (most recent call last):
    > #   File "test.py", line 8, in <module>
    > #     print msg.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'>
    >
    > If anybody could help me with the code, I would be very grateful. Also, if you
    > can criticize what I wrote, I would be very happy, as I would like to improve my
    > Python-fu (which is minimal, unfortunately).
    >
    > Thanks in advance, Rogério Brito.
    >
    > --
    > Rogério Brito : rbrito@{mackenzie,ime.usp}.br : GPG key 1024D/7C2CAEB8http://www.ime.usp.br/~rbrito:http://meusite.mackenzie.com.br/rbrito
    > Projects: algorithms.berlios.de : lame.sf.net : vrms.alioth.debian.org
    >
    >  drop-alternatives.py
    > 1KViewDownload
    >
    >  triggers-the-problem.mbx.gz
    > < 1KViewDownload


    You need to modify line 28 to read: body = part.get_payload()

    ~Sean
    Sean DiZazzo, Sep 9, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Bancroft
    Replies:
    2
    Views:
    2,397
    =?Utf-8?B?UmFodWwgQW5hbmQ=?=
    Dec 28, 2004
  2. =?Utf-8?B?RGFu?=
    Replies:
    7
    Views:
    1,012
  3. Alex Molochnikov
    Replies:
    0
    Views:
    442
    Alex Molochnikov
    Oct 6, 2005
  4. Roland

    Strange performance effects

    Roland, Jan 20, 2004, in forum: C++
    Replies:
    3
    Views:
    349
    Chris Theis
    Jan 20, 2004
  5. Avdi B. Grimm
    Replies:
    0
    Views:
    99
    Avdi B. Grimm
    Oct 5, 2005
Loading...

Share This Page