Amazing-Python-Scripts
48 строк · 2.1 Кб
1import ezgmail
2
3
4def attachmentdownload(resulthreads):
5# Two Objects used in code are GmailThread and GmailMessage
6# 1. GmailThread - Represents conversation threads
7# 2. GmailMessage - Represents individual emails within Threads
8countofresults = len(resulthreads)
9try:
10for i in range(countofresults):
11# checks whether the count of messages in threads is greater than 1
12if len(resulthreads[i].messages) > 1:
13for j in range(len(resulthreads[i].messages)):
14resulthreads[i].messages[
15j].downloadAllAttachments() # downloads attachment(s) for individual messages
16else:
17# downloads attachment(s) for single message
18resulthreads[i].messages[0].downloadAllAttachments()
19print("Download compelete. Please check your root directory.")
20except:
21raise Exception("Error occured while downloading attachment(s).")
22
23
24if __name__ == '__main__':
25query = input("Enter search query: ")
26# appending to make sure the result threads always has an attachment
27newquery = query + " + has:attachment"
28# search functions accepts all the operators described at https://support.google.com/mail/answer/7190?hl=en
29resulthreads = ezgmail.search(newquery)
30
31if len(resulthreads) == 0:
32# Executed if results don't have attachment
33print("Result has no attachments:")
34else:
35print("Result(s) with attachments:")
36for threads in resulthreads:
37# prints the subject line of email thread in results
38print(f"Email Subject: {threads.messages[0].subject}")
39try:
40ask = input(
41"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
42if ask == "Yes":
43# calls the function that downloads attachment(s)
44attachmentdownload(resulthreads)
45else:
46print("Program exited")
47except:
48print("Something went wrong")
49