-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_usage.py
70 lines (53 loc) · 2.35 KB
/
example_usage.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
import asyncio
from bs4 import BeautifulSoup
from temp_mail import AsyncTempMail
fetched_email_ids = []
def filter_emails(all_emails):
filtered_emails = []
for email in all_emails:
if email.email_id not in fetched_email_ids:
fetched_email_ids.append(email.email_id)
filtered_emails.append(email)
return filtered_emails
async def print_email(email):
print(f"\nFrom: {email.sender}")
print(f"Subject: {email.subject}")
print(f"Attachments count: {email.attachments['count']}")
print(f"\nBody: \n{email.text_body}")
if email.attachments["count"] > 0:
print("Attachments content:\n")
for file in email.attachments["files"]:
print(f"\nFilename: {file.filename}")
print(f"File size: {file.size}")
print(f"File mimetype: {file.mimetype}")
print("File content: ")
if file.mimetype == "text/plain":
async for chunk in file.download(): # use this to download any attachment
print(chunk.decode())
print()
continue
print("Mimetype not 'text/plain'... skipping")
async def main():
async with AsyncTempMail() as temp_mail:
response_json = await temp_mail.fetch_new_email_adress()
email_adress = response_json["mailbox"]
token = response_json[
"token" # use this token later to access emails associated with the email address
]
print(f"Temp email adress: {email_adress}\nTemp email adress token: {token}\n")
while True:
input("Press Enter to check for new emails: ")
filtered_emails = filter_emails(
await temp_mail.fetch_all_emails(token)
) # filter emails to avoid displaying the same ones next time
if len(filtered_emails) > 0:
print(f"You have {len(filtered_emails)} new email(s)!")
for email in filtered_emails:
detailed_email = (
await email.fetch_detailed_email()
) # you need to use the 'fetch_detailed_email' function to retrieve the details of the email
await print_email(detailed_email)
continue
print("You do not have any new emails. :(\n")
if __name__ == "__main__":
asyncio.run(main())