eudemonics

gmail version 1.1 - WORKING - unfinished

Sep 30th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. import email, getpass, imaplib, re, sys, os, os.path
  2.  
  3. count = 1
  4.  
  5. emailaddr = raw_input("enter email --> ")
  6.  
  7. #VALIDATE EMAIL USING REGEX
  8. match = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
  9.  
  10. if match:
  11.    count = 1
  12.    print 'email is valid'
  13.    print 'checking for gmail'
  14.  
  15. else:
  16.    count = 2
  17.    print 'invalid email address'
  18.    print 'bad attempts: ' + str(count)
  19.      
  20.    if count > 1 and count < 4:        
  21.       emailaddr = raw_input("enter email again --> ")
  22.       match2 = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
  23.       if match2:
  24.          print 'email is valid'
  25.          print 'checking for gmail'
  26.  
  27.       else:
  28.          count = 3
  29.          print 'invalid email address'
  30.          print 'bad attempts: ' + str(count)
  31.          emailaddr = raw_input("enter email again --> ")
  32.          match3 = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
  33.  
  34.          if match3:              
  35.             print 'email is valid'
  36.             print 'checking for gmail'
  37.  
  38.          else:
  39.             count = 4
  40.             print 'invalid email address'
  41.             print 'bad attempts: ' + str(count)
  42.  
  43.             if count > 3:
  44.                print 'too many bad attempts'
  45.                print 'aborting..'
  46.  
  47. emailpass = getpass.getpass("enter password --> ")
  48.  
  49. if 'gmail.com' in emailaddr:
  50.  
  51.    print 'GMAIL address found'
  52.    print 'attempting logon'
  53.  
  54.    # connecting to IMAP
  55.    server = imaplib.IMAP4_SSL('imap.gmail.com')
  56.    server.login(emailaddr, emailpass)
  57.    server.list()
  58.  
  59.    # CHOOSE INBOX OR ALL MAIL BY COMMENTING AND UNCOMMENTING BELOW
  60.    server.select('inbox') # connect to inbox
  61.    #server.select('[Gmail]/All Mail') # connect to all mail folder
  62.  
  63.    result, uids = server.uid('SEARCH', None, "ALL")
  64.    # search and return unique IDs (UIDs)
  65.  
  66.    for email_uid in uids[0].split():
  67.       result, data = server.uid('FETCH', email_uid, '(RFC822)')
  68.  
  69.       body = data[0][1] # raw text of entire email
  70.  
  71.       save_path = 'output'
  72.       user_savename = emailaddr.rstrip("@gmail.com")
  73.  
  74.       file_name = user_savename+".txt"
  75.  
  76.       complete_name = os.path.join(save_path, file_name)
  77.       file = open(complete_name, 'w')
  78.       file.write(body)
  79.       file.close()
  80.  
  81. savedfilename = emailaddr.rstrip("@gmail.com")+".txt"
  82.  
  83. print "file stored in subdirectory as " + savedfilename
  84.  
  85. #############
  86.    # TO FETCH MULTIPLE EMAILS AT ONCE
  87.  
  88.    #result, uids = server.uid('SEARCH', None, "ALL")
  89.  
  90.    #fetch_ids = ','.join(uids[0].split())
  91.    ##convert to comma separated list
  92.  
  93.    #result, data = server.uid('FETCH', fetch_ids, '(RFC822)')
  94.    ##return all emails
  95.  
  96.    #body = data[0][1]
  97.    #print body
  98. #############
  99.  
  100. #############
  101.    # TO GET LATEST EMAIL BY UID
  102.  
  103.    #latest_email_uid = items[0].split()[-1]
  104.  
  105.    #result, data = server.uid('FETCH', latest_email_uid, '(RFC822)')
  106.    ##fetch email body (RFC822) for latest UID
  107.  
  108.    #body = data[0][1] # raw text of entire email
  109.    #print body
  110. #############
  111.  
  112. #############
  113.    # TO SEARCH LATEST SEQUENTIAL ID INSTEAD OF UNIQUE ID
  114.  
  115.    #result, sids = mail.search(None, "ALL")
  116.    #ids = sids[0] # sids is a list
  117.    #id_list = ids.split() # ids is a space separated string
  118.    #latest_email_id = id_list[-1] # get the latest
  119.  
  120.    #result, data = mail.fetch(latest_email_id, "(RFC822)")
  121.    ## fetch the email body (RFC822) for the given ID
  122.  
  123.    #body = data[0][1] # raw text of entire email
  124. #############
  125.  
  126. print "done, exiting.."
  127.  
  128. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment