View difference between Paste ID: zPXLgV5k and kKgge9Fy
SHOW: | | - or go back to the newest paste.
1
import email, getpass, imaplib, re, sys, os, os.path
2
3-
count = 1
3+
count = 0
4
5
emailaddr = raw_input("enter email --> ")
6
emailpass = getpass.getpass("enter password --> ")
7
8
#VALIDATE EMAIL USING REGEX
9
match = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
10
11
if match:
12
   count = 0
13
   print 'email is valid'
14
   print 'checking for gmail'
15
16-
   count = 2
16+
17
   count = 1
18
   print 'invalid email address'
19
   print 'bad attempts: ' + str(count)
20-
   if count > 1 and count < 4:         
20+
21
   if count > 0 and count < 4:         
22
      emailaddr = raw_input("enter email again --> ")
23
      match2 = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
24
      if match2:
25
         print 'email is valid'
26
         emailpass = getpass.getpass("enter password --> ")
27
         print 'checking for gmail'
28-
         count = 3
28+
29
      else:
30
         count = 2
31
         print 'invalid email address'
32
         print 'bad attempts: ' + str(count)
33
         emailaddr = raw_input("enter email again --> ")
34
         match3 = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
35
36
         if match3:               
37
            print 'email is valid'
38
            emailpass = getpass.getpass("enter password --> ")
39-
            count = 4
39+
40
41
         else:
42
            count = 3
43
            print 'invalid email address'
44
            print 'bad attempts: ' + str(count)
45
46
            if count > 3:
47
               print 'too many bad attempts'
48
               print 'aborting..'
49
50
if 'gmail.com' in emailaddr:
51
52
   print 'GMAIL address found'
53
   print 'attempting logon'
54
55
   # connecting to IMAP
56-
   server.login(emailaddr, emailpass)
56+
57
58
   while True:
59
60
      try:
61
         loginstatus, logindata = server.login(emailaddr, emailpass)
62
         if loginstatus == 'OK':
63
            print 'login successful, fetching emails..'
64
65
      except:
66
         pass
67
         print 'error: login failure'
68
         emailaddr = raw_input("please enter email again --> ")
69
         matchcred = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
70
71
         if matchcred:
72
            emailpass = getpass.getpass("enter password --> ")
73
            print 'attempting logon again'
74-
      file_name = user_savename+".txt"
74+
            continue
75
76
         else:
77-
      file = open(complete_name, 'w')
77+
78-
      file.write(body)
78+
            emailaddr = raw_input("please enter email again --> ")
79-
      file.close()
79+
            matchcred = re.search(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', emailaddr)
80
            continue
81-
savedfilename = emailaddr.rstrip("@gmail.com")+".txt"
81+
82
      else:
83-
print "file stored in subdirectory as " + savedfilename
83+
         break
84
     
85
   server.list()
86
87
   # CHOOSE INBOX OR ALL MAIL BY COMMENTING AND UNCOMMENTING BELOW
88
   server.select('inbox') # connect to inbox
89
   #server.select('[Gmail]/All Mail') # connect to all mail folder
90
91
   result, uids = server.uid('SEARCH', None, "ALL")
92
93
   # search and return unique IDs (UIDs)
94
95
   for email_uid in uids[0].split():
96
97
      result, data = server.uid('FETCH', email_uid, '(RFC822)')
98
99
      body = data[0][1] # raw text of entire email
100
101
      save_path = 'output'
102
      user_savename = emailaddr.rstrip("@gmail.com")
103
      file_name = user_savename+"-"+email_uid+".txt"
104
      complete_name = os.path.join(save_path, file_name)
105
106
      if os.path.isfile(complete_name):
107
         print complete_name + ' already exists, skipping..'
108
         continue
109
110
      else:
111
         file = open(complete_name, 'w')
112
         file.write(body)
113
         file.close()
114
      
115
         print 'raw message data saved to new file: ' + complete_name
116
117
#############
118
   # TO FETCH MULTIPLE EMAILS AT ONCE
119
120
   #result, uids = server.uid('SEARCH', None, "ALL")
121
122
   #fetch_ids = ','.join(uids[0].split())
123
   ##convert to comma separated list
124
125
   #result, data = server.uid('FETCH', fetch_ids, '(RFC822)')
126-
print "done, exiting.."
126+
127
128
   #body = data[0][1]
129
   #print body
130
#############
131
132
#############
133
   # TO GET LATEST EMAIL BY UID
134
135
   #latest_email_uid = items[0].split()[-1]
136
137
   #result, data = server.uid('FETCH', latest_email_uid, '(RFC822)')
138
   ##fetch email body (RFC822) for latest UID
139
140
   #body = data[0][1] # raw text of entire email
141
   #print body
142
#############
143
144
#############
145
   # TO SEARCH LATEST SEQUENTIAL ID INSTEAD OF UNIQUE ID
146
147
   #result, sids = mail.search(None, "ALL") 
148
   #ids = sids[0] # sids is a list
149
   #id_list = ids.split() # ids is a space separated string 
150
   #latest_email_id = id_list[-1] # get the latest
151
152
   #result, data = mail.fetch(latest_email_id, "(RFC822)")
153
   ## fetch the email body (RFC822) for the given ID
154
 
155
   #body = data[0][1] # raw text of entire email
156
#############
157
158
   print 'inbox contents successfully saved to file. YAY!'
159
160
   print 'logging out..'
161
162
   server.logout()
163
164
   print "logout successful. exiting.."
165
166
else:
167
168
   print 'GMAIL address not found. quitting..'
169
170
sys.exit()