Guest User

Untitled

a guest
May 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import imaplib
  2. import email
  3.  
  4. #connect to gmail
  5.  
  6. mail = imaplib.IMAP4_SSL('imap.gmail.com')
  7. mail.login('email@gmail.com','yourPassWordPlease')
  8. mail.select('inbox')
  9. mail.list()
  10.  
  11. typ, data = mail.search(None, 'ALL')
  12. for num in data[0].split():
  13. typ, data = mail.fetch(num, '(RFC822)')
  14. typ, data = mail.search(None, 'ALL')
  15. ids = data[0]
  16. id_list = ids.split()
  17.  
  18. # get most recent email id
  19. # Any Emails?
  20. if id_list:
  21. latest_email_id = int( id_list[-1] )
  22. for i in range( latest_email_id, latest_email_id-1, -1):
  23. typ, data = mail.fetch( i, '(RFC822)')
  24. for response_part in data:
  25. if isinstance(response_part, tuple):
  26. msg = email.message_from_string(response_part[1])
  27. varSubject = msg['subject']
  28. varFrom = msg['from']
  29. varFrom = varFrom.replace('<','')
  30. varFrom = varFrom.replace('>','')
  31. if len( varSubject ) >35: # Subject to large - turn the light off
  32. varSubject = '000'
  33. else: #No Emails so turn the light off
  34. varSubject = '000'
  35.  
  36. #print the subject to test
  37.  
  38. print varSubject
  39.  
  40. #output the subject to the ledborg
  41.  
  42. LedBorg = open('/dev/ledborg', 'w')
  43. LedBorg.write(varSubject)
  44. del LedBorg
  45. #Remove used emails from mailbox
  46. typ, data = mail.search(None, 'ALL')
  47. for num in data[0].split():
  48. mail.store(num, '+FLAGS', '\\Deleted')
  49. mail.expunge()
  50. mail.close()
  51. mail.logout()
Add Comment
Please, Sign In to add comment