Oct-06-2018, 09:23 AM
(This post was last modified: Oct-07-2018, 10:54 AM by Pedroski55.)
I'm trying to get the text body of emails. To that end I have this code:
The function prints the following, the lower part of which is what I want, starting with the student number, then name, then answers:
I can see what I want before my eyes, but I can't get it!!
Can someone please help me get the text body as a string or list, or just point me in the right direction??
def read(username, password, sender_of_interest=None): # Login to INBOX imap = imaplib.IMAP4_SSL("imap.qq.com", 993) imap.login(username, password) imap.select('INBOX') # Use search(), not status() # Print all unread messages from a certain sender of interest if sender_of_interest: status, response = imap.uid('search', None, 'UNSEEN', 'FROM {0}'.format(sender_of_interest)) else: status, response = imap.uid('search', None, 'UNSEEN') if status == 'OK': unread_msg_nums = response[0].split() else: unread_msg_nums = [] data_list = [] for e_id in unread_msg_nums: data_dict = {} e_id = e_id.decode('utf-8') _, response = imap.uid('fetch', e_id, '(RFC822)') html = response[0][1].decode('utf-8') email_message = email.message_from_string(html) #data_dict['mail_to'] = email_message['To'] #data_dict['mail_subject'] = email_message['Subject'] #data_dict['mail_from'] = email.utils.parseaddr(email_message['From']) data_dict['body'] = email_message.get_payload()[0]#.get_payload(decode=True) data_list.append(data_dict) print(data_list) print(data_dict['body']) emailData = read('[email protected]', 'myIMAPlogin')The output seems to be a list of dictionaries, I've tried lots of ways to access the data, but I mostly get something like:
Quote:emailData['body']
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
emailData['body']
TypeError: 'NoneType' object is not subscriptable
The function prints the following, the lower part of which is what I want, starting with the student number, then name, then answers:
Quote:[{'body': <email.message.Message object at 0x7f0c5c4d2be0>}, {'body': '1'}, {'body': <email.message.Message object at 0x7f0c5c4d2e80>}]
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
1725010103
朱素梅
and
for
to
over
up
around
in
about
beneath
down
I can see what I want before my eyes, but I can't get it!!
Can someone please help me get the text body as a string or list, or just point me in the right direction??