Skip to content

Commit 5bffac6

Browse files
Merge pull request avinashkranjan#624 from Kirtan17/issue-482
Issue 482
2 parents a92ac0d + a141011 commit 5bffac6

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

Attachment_Downloader/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Attachment Downloader
2+
3+
The script downloads gmail atttachment(s) in no time!
4+
5+
6+
# Setup Instructions
7+
8+
The script uses ezgmail module (to know more visit https://pypi.org/project/EZGmail/). To install please type the following command in your bash.
9+
10+
```bash
11+
pip install EZGmail
12+
```
13+
14+
Once the module is installed you will need to download credentials.json file by going to [developers.google.com](https://developers.google.com/gmail/api/quickstart/python) and clicking the Enable the Gmail API button (select "Desktop app" as OAuth Client in second step).
15+
16+
Once you have credentials.json (in root directory of project folder), the first time you run the script it will open up a browser window asking you to log in to your Gmail account and allow "Quickstart" app to access it. A token.json file will be generated (in root directory of project folder) which your script can use to access your account.
17+
18+
# Outputs
19+
20+
## Output 1
21+
22+
![image](https://user-images.githubusercontent.com/75886245/111307477-82e5d100-867f-11eb-8461-58abd04fa56a.png)
23+
24+
## Output 2
25+
26+
![image](https://user-images.githubusercontent.com/75886245/111307625-ba547d80-867f-11eb-8f3b-77ded9e72416.png)
27+
28+
## Output 3
29+
30+
![image](https://user-images.githubusercontent.com/75886245/111307771-e66ffe80-867f-11eb-8bbe-457675fcb963.png)
31+
32+
## Output 4
33+
34+
![image](https://user-images.githubusercontent.com/75886245/111307864-0273a000-8680-11eb-8400-3009dff8d7db.png)
35+
36+
# Author
37+
38+
Contributed by Kirtan Bhatt
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import ezgmail
2+
3+
def attachmentdownload(resulthreads):
4+
# Two Objects used in code are GmailThread and GmailMessage
5+
# 1. GmailThread - Represents conversation threads
6+
# 2. GmailMessage - Represents individual emails within Threads
7+
countofresults = len(resulthreads)
8+
try:
9+
for i in range(countofresults):
10+
if len(resulthreads[i].messages) > 1: # checks whether the count of messages in threads is greater than 1
11+
for j in range(len(resulthreads[i].messages)):
12+
resulthreads[i].messages[
13+
j].downloadAllAttachments() # downloads attachment(s) for individual messages
14+
else:
15+
resulthreads[i].messages[0].downloadAllAttachments() # downloads attachment(s) for single message
16+
print("Download compelete. Please check your root directory.")
17+
except:
18+
raise Exception("Error occured while downloading attachment(s).")
19+
20+
21+
if __name__ == '__main__':
22+
query = input("Enter search query: ")
23+
newquery = query + " + has:attachment" # appending to make sure the result threads always has an attachment
24+
resulthreads = ezgmail.search(newquery) # search functions accepts all the operators described at https://support.google.com/mail/answer/7190?hl=en
25+
26+
if len(resulthreads) == 0:
27+
print("Result has no attachments:") # Executed if results don't have attachment
28+
else:
29+
print("Result(s) with attachments:")
30+
for threads in resulthreads:
31+
print(f"Email Subject: {threads.messages[0].subject}") # prints the subject line of email thread in results
32+
try:
33+
ask = input(
34+
"Do you want to download attachment(s) in result(s) (Yes/No)? ") # Allows user to decide whether they want to download attachment(s) or not
35+
if ask == "Yes":
36+
attachmentdownload(resulthreads) # calls the function that downloads attachment(s)
37+
else:
38+
print("Program exited")
39+
except:
40+
print("Something went wrong")
41+
42+
43+
44+

0 commit comments

Comments
 (0)