Search code examples
pythonpython-2.7python-3.xgmail

Read data of type 'instance' in python


I am using this library to read gmail inbox in python. This is the response I'm getting per mail :

From nobody Mon Dec 26 16:42:46 2016
Delivered-To: [email protected]
Received: by 10.28.211.66 with SMTP id ferf98er9fef9fr;
.
.
.
X-Source-Dir: erferfefefrref:/public_html
X-CMAE-Envelope: grtgrtgrtgrtgrt......

This is the message body

On checking the object type of the response, it returns <type 'instance'>

I am able to read properties like Subject, Delivered-To, Received, X-Source-Dir using this code

print response['subject']
print response['delivered-to']

But cannot read the message body ( This is the message body in the given example )

The documentation of the library says we can get the body using

print response.body

But that doesn't seem to work, and gives this error instead :

Message instance has no attribute 'body'

Is there any other way I can extract the body from a data like above ??


Solution

  • From the page you linked in your question:

    unread = g.inbox().mail(unread=True)
    print unread[0].body
    # None
    
    unread[0].fetch()
    print unread[0].body
    

    Is this not working for you?