Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import email, getpass, imaplib, re, sys, os, os.path
- count = 0
- emailaddr = raw_input("enter email --> ")
- emailpass = getpass.getpass("enter password --> ")
- #VALIDATE EMAIL USING REGEX
- match = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
- if match:
- count = 0
- print 'email is valid'
- print 'checking for gmail'
- else:
- count = 1
- print 'invalid email address'
- print 'bad attempts: ' + str(count)
- if count > 0 and count < 4:
- emailaddr = raw_input("enter email again --> ")
- match2 = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
- if match2:
- print 'email is valid'
- emailpass = getpass.getpass("enter password --> ")
- print 'checking for gmail'
- else:
- count = 2
- print 'invalid email address'
- print 'bad attempts: ' + str(count)
- emailaddr = raw_input("enter email again --> ")
- match3 = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
- if match3:
- print 'email is valid'
- emailpass = getpass.getpass("enter password --> ")
- print 'checking for gmail'
- else:
- count = 3
- print 'invalid email address'
- print 'bad attempts: ' + str(count)
- if count > 3:
- print 'too many bad attempts'
- print 'aborting..'
- if 'gmail.com' in emailaddr:
- print 'GMAIL address found'
- print 'attempting logon'
- # connecting to IMAP
- server = imaplib.IMAP4_SSL('imap.gmail.com')
- while True:
- try:
- loginstatus, logindata = server.login(emailaddr, emailpass)
- if loginstatus == 'OK':
- print 'login successful, fetching emails..'
- except:
- pass
- print 'error: login failure'
- emailaddr = raw_input("please enter email again --> ")
- matchcred = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
- if matchcred:
- emailpass = getpass.getpass("enter password --> ")
- print 'attempting logon again'
- continue
- else:
- print 'invalid email address'
- emailaddr = raw_input("please enter email again --> ")
- matchcred = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
- continue
- else:
- break
- server.list()
- # CHOOSE INBOX OR ALL MAIL BY COMMENTING AND UNCOMMENTING BELOW
- server.select('inbox') # connect to inbox
- #server.select('[Gmail]/All Mail') # connect to all mail folder
- result, uids = server.uid('SEARCH', None, "ALL")
- # search and return unique IDs (UIDs)
- for email_uid in uids[0].split():
- result, data = server.uid('FETCH', email_uid, '(RFC822)')
- body = data[0][1] # raw text of entire email
- save_path = 'output'
- user_savename = emailaddr.rstrip("@gmail.com")
- file_name = user_savename+"-"+email_uid+".txt"
- complete_name = os.path.join(save_path, file_name)
- if os.path.isfile(complete_name):
- print complete_name + ' already exists, skipping..'
- continue
- else:
- file = open(complete_name, 'w')
- file.write(body)
- file.close()
- print 'raw message data saved to new file: ' + complete_name
- #############
- # TO FETCH MULTIPLE EMAILS AT ONCE
- #result, uids = server.uid('SEARCH', None, "ALL")
- #fetch_ids = ','.join(uids[0].split())
- ##convert to comma separated list
- #result, data = server.uid('FETCH', fetch_ids, '(RFC822)')
- ##return all emails
- #body = data[0][1]
- #print body
- #############
- #############
- # TO GET LATEST EMAIL BY UID
- #latest_email_uid = items[0].split()[-1]
- #result, data = server.uid('FETCH', latest_email_uid, '(RFC822)')
- ##fetch email body (RFC822) for latest UID
- #body = data[0][1] # raw text of entire email
- #print body
- #############
- #############
- # TO SEARCH LATEST SEQUENTIAL ID INSTEAD OF UNIQUE ID
- #result, sids = mail.search(None, "ALL")
- #ids = sids[0] # sids is a list
- #id_list = ids.split() # ids is a space separated string
- #latest_email_id = id_list[-1] # get the latest
- #result, data = mail.fetch(latest_email_id, "(RFC822)")
- ## fetch the email body (RFC822) for the given ID
- #body = data[0][1] # raw text of entire email
- #############
- print 'inbox contents successfully saved to file. YAY!'
- print 'logging out..'
- server.logout()
- print "logout successful. exiting.."
- else:
- print 'GMAIL address not found. quitting..'
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement