Advertisement
metalx1000

check imap mail gmail

Jul 28th, 2015
2,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. #turn on "Less Secure Apps"
  2. https://www.google.com/settings/security/lesssecureapps
  3.  
  4. #do a quick check
  5. curl -u username:password --silent "https://mail.google.com/mail/feed/atom" |sed 's/</\n</g'
  6.  
  7. #enable POP or IMAP
  8. https://mail.google.com/mail/u/0/#settings/fwdandpop
  9.  
  10. ##user this python code##
  11. #!/usr/bin/env python
  12. import email, getpass, imaplib, os
  13.  
  14. detach_dir = '.' # directory where to save attachments (default: current)
  15. #user = raw_input("Enter your GMail username:")
  16. #pwd = getpass.getpass("Enter your password: ")
  17. user = "username"
  18. pwd = 'password'
  19.  
  20. # connecting to the gmail imap server
  21. m = imaplib.IMAP4_SSL("imap.gmail.com")
  22. m.login(user,pwd)
  23. m.select("[Gmail]/All Mail") # here you a can choose a mail box like INBOX instead
  24. # use m.list() to get all the mailboxes
  25.  
  26. resp, items = m.search(None, "ALL") # you could filter using the IMAP rules here (check http://www.example-code.com/csharp/imap-search-critera.asp)
  27. items = items[0].split() # getting the mails id
  28.  
  29. for emailid in items:
  30.     resp, data = m.fetch(emailid, "(RFC822)") # fetching the mail, "`(RFC822)`" means "get the whole stuff", but you can ask for headers only, etc
  31.     email_body = data[0][1] # getting the mail content
  32.     mail = email.message_from_string(email_body) # parsing the mail content to get a mail object
  33.  
  34.     #Check if any attachments at all
  35.     if mail.get_content_maintype() != 'multipart':
  36.         continue
  37.  
  38.     print "["+mail["From"]+"] :" + mail["Subject"]
  39.  
  40.     #check if mail is from certain user
  41.     #if mail["From"] == "Person <person@gmail.com>":
  42.     #  print email_body
  43.  
  44.  
  45.     # we use walk to create a generator so we can iterate on the parts and forget about the recursive headach
  46.     for part in mail.walk():
  47.         # multipart are just containers, so we skip them
  48.         if part.get_content_maintype() == 'multipart':
  49.             continue
  50.  
  51.         # is this part an attachment ?
  52.         if part.get('Content-Disposition') is None:
  53.             continue
  54.  
  55.         filename = part.get_filename()
  56.         counter = 1
  57.  
  58.         # if there is no filename, we create one with a counter to avoid duplicates
  59.         if not filename:
  60.             filename = 'part-%03d%s' % (counter, 'bin')
  61.             counter += 1
  62.  
  63.         att_path = os.path.join(detach_dir, filename)
  64.  
  65.         #Check if its already there
  66.         if not os.path.isfile(att_path) :
  67.             # finally write the stuff
  68.             fp = open(att_path, 'wb')
  69.             fp.write(part.get_payload(decode=True))
  70.             fp.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement