forked from ashishkg0022/gmail-attachments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
56 lines (37 loc) · 1.32 KB
/
script.py
File metadata and controls
56 lines (37 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import email
import getpass
import imaplib, os , sys
curr_dir ='.'
user = input("Enter your GMail username:")
password =getpass.getpass("Enter your password: ")
search_email=input("Enter mail of which attachments has to be downloaded")
if search_email not in os.listdir(curr_dir):
os.mkdir(search_email)
curr_dir = './'+search_email
m = imaplib.IMAP4_SSL("imap.gmail.com",port=993)
try :
m.login(user,password)
except imaplib.IMAP4.error as e :
print ("login failed !!" + str(e) )
sys.exit(0)
m.select( mailbox='INBOX')
resp, items = m.uid('search', None , 'FROM' , search_email)
items = items[0].split()
for emailid in items:
resp, data = m.uid('fetch', emailid , "(RFC822)")
raw_email = data[0][1]
mail = email.message_from_bytes(raw_email)
#body = mail.get_body()
#print ("["+mail["From"]+"] :" + mail["Subject"])
if mail.get_content_maintype() != 'multipart':
continue
for part in mail.walk():
if(part.get('Content-Disposition' ) is not None ) :
filename = part.get_filename()
final_path= os.path.join(curr_dir + filename)
if not os.path.isfile(final_path) :
fp = open(curr_dir+"/"+(filename), 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
m.close()
m.logout()