AttributeError: 'file' object has no attribute 'encode'
Question: 
    Traceback (most recent call last):
      File "E:\blahblahblah\emailsend.py", line 26, in <module>
        msg.attach(MIMEText(file))
      File "E:\blahblahblah\Python 2.7.11\lib\email\mime\text.py", line 30, in __init__
        self.set_payload(_text, _charset)
      File "E:\blahblahblah\Python 2.7.11\lib\email\message.py", line 226, in set_payload
        self.set_charset(charset)
      File "E:\blahblahblah\Python 2.7.11\lib\email\message.py", line 268, in set_charset
        cte(self)
      File "E:\blahblahblah\Python 2.7.11\lib\email\encoders.py", line 73, in encode_7or8bit
        orig.encode('ascii')
    AttributeError: 'file' object has no attribute 'encode'https://stackoverflow.com/questions/ask#

I've been looking this up a lot but I haven't found an answer.

The only important parts of the code is this:

    
    
    file = open('newfile.txt')
    
    msg.attach(MIMEText(file))

There are other parts but I've debugged it and I get the error at the
'msg.attach(MIMEText(file))' line.

Any help?


Answer: MIMEText takes the _content_ of the file, not the file object.

    
    
    msg.attach(MIMEText(open("newfile.txt").read()))
    

