Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading mail getting [<email.message.Message object at 0x02209970>, ...

241 views
Skip to first unread message

Abdur-Rahmaan Janhangeer

unread,
Nov 14, 2019, 2:44:07 PM11/14/19
to
Greetings,

Using this code to read mail. I'm printing to file only mails sent by some
people. For some mails i get the body as the below instead of actual text:

[<email.message.Message object at 0x02209970>, <email.message.Message
object at 0x021ECB30>]

instead of the actual mail body.

Here is the code:

#
#
import imaplib
import email
import time

my_senders = ['y...@x.com ', 'y...@y.com', 'y...@z.com']
my_mail_count = 1
open('data.txt', 'w+')
def read_email_from_gmail():
global my_mail_count, my_senders

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('mym...@gmail.com','password')
mail.select('inbox')

result, data = mail.search(None, 'ALL')
mail_ids = data[0]

id_list = mail_ids.split()
first_email_id = int(id_list[0])
latest_email_id = int(id_list[-1])

for i in range(latest_email_id,first_email_id, -1):
#pypi # need str(i)
result, data = mail.fetch(str(i), '(RFC822)' )

for response_part in data:
if isinstance(response_part, tuple):
# from_bytes, not from_string
msg = email.message_from_bytes(response_part[1])

email_subject = msg['subject']
email_from = msg['from']
print ('{} {}'.format(my_mail_count, email_subject))
print(' {}'.format(email_from))
my_mail_count += 1

#email_body = msg.get_payload(decode=True)

for m in my_senders:
if m in email_from:
if msg.is_multipart():
for part in msg.get_payload():
print(msg.get_payload(),
file=open('data.txt', 'a'))
if isinstance(msg.get_payload(), list):
print(dir(msg.get_payload()[0]))
else:
print(msg.get_payload(), file=open('data.txt',
'a'))
if isinstance(msg.get_payload(), list):
print(dir(msg.get_payload()[0]))

read_email_from_gmail()
#
#

Any idea?

Yours,

Abdur-Rahmaan Janhangeer
pythonmembers.club <http://www.pythonmembers.club/> | github
<https://github.com/Abdur-rahmaanJ>
Mauritius

MRAB

unread,
Nov 14, 2019, 4:09:31 PM11/14/19
to
The payload is sometimes split into parts that are encoded. Do something
like this:

from email.header import decode_header

def decode_payload(header, default_encoding='utf-8'):
parts = []

for data, encoding in decode_header(header):
if isinstance(data, str):
parts.append(data)
else:
parts.append(data.decode(encoding or default_encoding))

return ' '.join(parts)

Abdur-Rahmaan Janhangeer

unread,
Dec 13, 2019, 12:26:38 PM12/13/19
to
Can you please indicate where is the header in my code above? Thanks.

dieter

unread,
Dec 14, 2019, 3:50:03 AM12/14/19
to
Abdur-Rahmaan Janhangeer <arj.p...@gmail.com> writes:

> Can you please indicate where is the header in my code above? Thanks.

Python is an object oriented language. As a consequence,
Python functions often return complex objects and not only
elementary objects. Use the documentation of the complex object's class
to find out how to work with them.

In general, Python has two functions to get a readable
description for an object: "repr" and "str".
"<email.message.Message object at 0x02209970>" is a typical
return value for "repr". Apply "str" to this message object
to get a representation which likely fits better your expectations.

Read the documentation for "email.message.Message" to learn how
to work with those "Message" objects on a more granular level.

0 new messages