Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29748

Read Latest Hotmail Emails in Python

$
0
0

I've been searching for a way to get the latest emails from my hotmail account (specifically the FROM and MESSAGE) using Python. The solutions mostly seem to be for gmail which isn't working as I would like.

Example 1: Using the Gmail examples - msg spits out a lot of unwanted data and the attempt to get subject, to and from returns blanks for each.

import imaplib
import email
from email.mime.multipart import MIMEMultipart

mail = imaplib.IMAP4_SSL('outlook.office365.com')
mail.login('myemail@hotmail.com', 'password')
mail.list()
mail.select('inbox')

for i in range(1, 5):
    typ, msg_data = mail.fetch(str(i), '(RFC822)')
    for response_part in msg_data:
        if isinstance(response_part, tuple):
            # print(response_part[1])
            msg = email.message_from_string(str(response_part[1]))
            print(msg)
            for header in [ 'subject', 'to', 'from' ]:
                print('%-8s: %s' % (header.upper(), msg[header]))

mail.close()
mail.logout()

Example 2: Gets last outlook email contents but cannot seem to get more (e.g. last 5)

import imaplib
msrvr = imaplib.IMAP4_SSL('outlook.office365.com', 993)

unm = 'myemail@hotmail.com'
pwd = 'password'
msrvr.login(unm, pwd)

print(str(len(msrvr.select('inbox'))))

stat,cnt = msrvr.select('inbox')

print(str(len(cnt)))

for i in range(0,5):

    stat,dta = msrvr.fetch(cnt[i], '(BODY[TEXT])')
    print(dta[0][1])

msrvr.close()
msrvr.logout()

Any thoughts how I could get the last 5 emails with FROM and MESSAGE?


Viewing all articles
Browse latest Browse all 29748

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>