classification
Title: Failure when calling __str__ for MIMEBase(message, rfc822) objects
Type: crash Stage:
Components: Library (Lib) Versions: Python 2.6, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: elachuni, facundobatista, hsp
Priority: normal Keywords:

Created on 2007-12-05 15:51 by hsp, last changed 2008-06-21 21:12 by facundobatista. This issue is now closed.

Files
File name Uploaded Description Edit
MimebaseError.py hsp, 2007-12-05 15:51
Messages (3)
msg58216 - (view) Author: Hendrik Spiegel (hsp) Date: 2007-12-05 15:51
When creating a MIMEBase object with message/rfc822 mimetype invoking
the objects __str__ method results in an exception. 
Even if this behaviour should be intended the error message "TypeError:
Expected list, got <type 'str'>" is not helpful. 
To reproduce the problem run the attached script after creating the file
 'test.eml' in the script's directory.


    mimetype = mimetypes.guess_type(filename)[0]
    maintype, subtype = mimetype.split('/')
    attachment = MIMEBase(maintype, subtype)
    attachment.set_payload(file(filename).read( ))
    print attachment

#python MimebaseError.py                                               
                                                                       
             [1]
message/rfc822
Traceback (most recent call last):
  File "MimebaseError.py", line 19, in <module>
    main()
  File "MimebaseError.py", line 16, in main
    print attachment
  File "email/message.py", line 116, in __str__
  File "email/message.py", line 131, in as_string
  File "email/generator.py", line 84, in flatten
  File "email/generator.py", line 109, in _write
  File "email/generator.py", line 135, in _dispatch
  File "email/generator.py", line 266, in _handle_message
  File "email/message.py", line 185, in get_payload
TypeError: Expected list, got <type 'str'>
msg68542 - (view) Author: Anthony Lenton (elachuni) * Date: 2008-06-21 21:05
I don't really think the MIMEBase class is supposed to be used like
this, or at all: it behaves more like a Multipart message in that it
expects to carry a list of MIMEBase objects as a payload (not a string!)
though it doesn't define a boundary by default.

You can carry on using the MIMEBase class, but changing the line that says:

    attachment.set_payload(file(filename).read( ))

for:

    attachment.set_payload([MIMEText(file(filename).read())])

I'd recommend you to use:

    attachment = Message()
    attachment.set_payload(file(filename).read())

or if you want a multipart message:

    attachment = MIMEMultipart()
    text = MIMEText(file(filename).read())
    attachment.attach(text)
msg68546 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-06-21 21:12
I agree with Anthony. If you have any further questions regarding how to
use this library feel free to ask them in python-list.
History
Date User Action Args
2008-06-21 21:12:41facundobatistasetstatus: open -> closed
resolution: not a bug
messages: + msg68546
nosy: + facundobatista
2008-06-21 21:05:31elachunisetnosy: + elachuni
messages: + msg68542
2008-01-20 19:55:31christian.heimessetpriority: normal
versions: + Python 2.6, - Python 2.4
2007-12-05 15:51:16hspcreate