-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
101 lines (88 loc) · 3.43 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# ===Python Script to Download Attachments From Mail Automatically ===
# Import the required packages (* mandatory input fields)
"""
1. **imaplib**
2. **base64**
3. **os**
4. **email**
5. **argparse**
"""
import imaplib
import base64
import os
import email
import argparse
# ==== Note: ====
"""
1. **-d** - Path To Downloaded File *
2. **-m** - User MailId *
3. **-p** - Mail Password *
4. **-f** - File Name
5. **-imap** - Imap Server Name *
6. **-port** - Imap Server Port *
"""
# ==== main function ====
def main(args):
# Provide MailId and Password from which need to login
email_user = args.mail_id
email_passwd = args.password
# Imap Server and Port
mail = imaplib.IMAP4_SSL(args.imap_server, args.imap_port)
# login with user details
mail.login(email_user, email_passwd)
"""
Select the Folder Name.
For example I used **Indox** here.
The parameter **readonly** is optional if it is assign to **True** it make the **unread** unread else **read**.
"""
mail.select("Inbox", readonly=True)
"""
Search for particular **Subject** and **From Id** in the mail list .
**Unseen** is an optional Keyword if we provide it will also search in ***unseen*** mail list.
Here , I searched for **From Id** - Rupesh with **subject** - SQL.
"""
type, data = mail.search(
None, '(FROM "Rupesh" SUBJECT "SQL")', 'UnSeen')
"""
Reverse the mail list from latest to older .
Incase you need all mail remove break statement.
"""
# Fetch Mail details for last sent mail only
for num in data[0].split()[::-1]:
typ, data = mail.fetch(num, '(RFC822)')
raw_email = data[0][1]
break
# Decode the Mail String to readable Data Format
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
# Download the file from mail
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
# Get the filename as same as mail attachment (Change if u Needed)
fileName = part.get_filename()
# In case of renaming the file provide the fileName (in arguments).
if args.filename:
fileName = args.filename
if bool(fileName):
# Specify the Download location Path
filePath = os.path.join(
args.target_dir, fileName)
# Save the file in Specified Location
if not os.path.isfile(filePath):
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Download Attachments From Mail')
parser.add_argument('-d', '--target_dir', required=True, help='path_to_save')
parser.add_argument('-m', '--mail_id', required=True, help='mailId')
parser.add_argument('-p', '--password', required=True, help='mail password')
parser.add_argument('-f', '--filename', help='FileName for file')
parser.add_argument('-imap','--imap_server',default="imap.gmail.com", help='Imap Server Name')
parser.add_argument('-port','--imap_port',default="993", help='Imap Server Port')
args = parser.parse_args()
# defined in ***line 29*** (top)
main(args)