MIMEText encode error - Python 2.6.6

MRAB python at mrabarnett.plus.com
Wed Sep 7 00:05:20 CEST 2011


On 06/09/2011 22:52, neubyr wrote:
> I am trying to write a program which can email file's content using
> smtplib. I am getting following error while using Python 2.6.6
> version.
>
> {{{
> File "./killed_jobs.py", line 88, in sendmail
>     msg = MIMEText(ipfile.read, 'plain')
>   File "/home/ssp/sge/python/2.6.6/lib/python2.6/email/mime/text.py",
> line 30, in __init__
>     self.set_payload(_text, _charset)
>   File "/home/ssp/sge/python/2.6.6/lib/python2.6/email/message.py",
> line 224, in set_payload
>     self.set_charset(charset)
>   File "/home/ssp/sge/python/2.6.6/lib/python2.6/email/message.py",
> line 266, in set_charset
>     cte(self)
>   File "/home/ssp/sge/python/2.6.6/lib/python2.6/email/encoders.py",
> line 73, in encode_7or8bit
>     orig.encode('ascii')
> AttributeError: 'builtin_function_or_method' object has no attribute 'encode'
>
> }}}
>
>
> I am referring to email examples on the doc site
> http://docs.python.org/release/2.6.6/library/email-examples.html#email-examples
> . Following is the msg object part in my code:
>
> {{{
> ...
> ...
> def sendmail(inputfile):
>          ipfile = open(inputfile, 'r')
>          msg = MIMEText(ipfile.read, 'plain')
>          ipfile.close()
>
> ...
> ...
> }}}
>
> I have tried setting subtype and chartype separately as mentioned here
> - http://docs.python.org/release/2.6.6/library/email.mime.html, but
> the error remains same. Any help on what might be wrong here?
>
The docs say:

     MIMEText(_text[, _subtype[, _charset]])

     ... _text is the string for the payload ...

You're passing ipfile.read, which is the read method of the file. You
should be passing the string returned by calling ipfile.read, ie
ipfile.read().



More information about the Python-list mailing list