Guest User

Untitled

a guest
Jan 10th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import imaplib
  4. import email
  5.  
  6. # Connect to imap server
  7. username = 'user@example.com'
  8. password = 'password'
  9. mail = imaplib.IMAP4_SSL('outlook.office365.com')
  10. mail.login(username, password)
  11.  
  12. # retrieve a list of the mailboxes and select one
  13. result, mailboxes = mail.list()
  14. mail.select("inbox")
  15.  
  16. # retrieve a list of the UIDs for all of the messages in the select mailbox
  17. result, numbers = mail.uid('search', None, 'ALL')
  18. uids = numbers[0].split()
  19.  
  20. # retrieve the headers (without setting the 'seen' flag) of the last 10 messages
  21. # in the list of UIDs
  22. result, messages = mail.uid('fetch', ','.join(uids[-10:]), '(BODY.PEEK[HEADER])')
  23.  
  24. # Convert the messages into email message object and print out the sender and
  25. # the subject.
  26. for _, message in messages[::2]:
  27. msg = email.message_from_string(message)
  28. print('{}: {}'.format(msg.get('from'), msg.get('subject')))
Add Comment
Please, Sign In to add comment