-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemail-attachment-downloader.py
51 lines (45 loc) · 1.82 KB
/
email-attachment-downloader.py
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
import imaplib
import base64
import os
import email
parser = argparse.ArgumentParser(description='Download email attachments')
parser.add_argument('--user', help='Gmail user.')
parser.add_argument('--password', help='Gmail app password.')
parser.add_argument('--folder', help='IMAP folder/label.')
args = parser.parse_args()
mailbox = imaplib.IMAP4_SSL('imap.gmail.com')
mailbox.login(args.user, args.password)
mailbox.select(args.folder)
type, data = mailbox.search(None, 'ALL')
mail_ids = data[0]
id_list = mail_ids.split()
for num in data[0].split():
typ, data = mailbox.fetch(num, '(RFC822)' )
raw_email = data[0][1]
# converts byte literal to string removing b''
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
# downloading attachments
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
#FIXME this just drops the attachment in the same dir as this python script
filePath = os.path.join(os.getcwd(), fileName)
if not os.path.isfile(filePath) :
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
subject = str(email_message).split("Subject: ", 1)[1].split("\nTo:", 1)[0]
print('Downloaded "{file}" from email titled "{subject}" with UID {uid}.'.format(file=fileName, subject=subject, uid=num.decode('utf-8')))
# move to trash
mailbox.store(num, '+X-GM-LABELS', '\\Trash')
# empty trash
mailbox.select('[Gmail]/Trash')
mailbox.store("1:*", '+FLAGS', '\\Deleted')
mailbox.expunge()
mailbox.close()
mailbox.logout()