def read_message(assistant, instance_vlc, player_vlc, username, password, sender_of_interest=None):
imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
imap.login(username, password)
imap.select('INBOX')
if sender_of_interest is None:
status, response = imap.search(None, '(UNSEEN)')
unread_msg_nums = response[0].split()
else:
status, response = imap.search(None, '(UNSEEN)', '(FROM "%s")' % (sender_of_interest))
unread_msg_nums = response[0].split()
if unread_msg_nums:
assistant.speak("Very good! Please wait, my assistant will read your messages!")
else:
assistant.speak("You do not have new messages!")
for e_id in unread_msg_nums:
_, header = imap.fetch(e_id, '(UID BODY[HEADER])')
header = header[0][-1]
header = "".join(header.split("\r"))
header = [h for h in header.split("\n") if h != ""]
subject = header[-1]
sender = header[-3].split("@")[0]
_, text = imap.fetch(e_id, '(UID BODY[1])')
text = text[0][1]
text = "Content :"+text
message = sender + ". " + subject + ". " + text
read_nicely_text(message, instance_vlc, player_vlc)
# Mark them as seen
for e_id in unread_msg_nums:
imap.store(e_id, '+FLAGS', '\Seen')
评论列表
文章目录