-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
99 lines (75 loc) · 2.92 KB
/
test.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
# modules
import imaplib
import email
import re
import html_text
import pyttsx3
from email.header import decode_header
username = "[email protected]"
# generated app password
app_password = "tyirdyxbiunstotc"
gmail_host = 'imap.gmail.com'
# select inbox
def get_mail(mail, selected_mails):
specialChars = "=-_"
for num in selected_mails[0].split():
_, data = mail.fetch(num, '(RFC822)')
# noinspection PyTupleAssignmentBalance
_, bytes_data = data[0]
# convert the byte data to message
email_message = email.message_from_bytes(bytes_data)
print("\n===========================================")
# access data
mail_subject = decode_header(email_message['subject'])
if type(mail_subject[0][0]) == str:
mail_subject = mail_subject[0][0]
else:
mail_subject = mail_subject[0][0].decode("utf-8")
mail_from = email_message["from"]
print("Subject: ", mail_subject)
print("To:", email_message["to"])
print("From: ", mail_from)
print("Date: ", email_message["date"])
for part in email_message.walk():
if part.get_content_type() == "text/plain" or part.get_content_type() == "text/html":
message = part.get_payload(decode=True)
message = message.decode()
# Converting HTML to plain Text
try:
if "<!DOCTYPE html>" in str(message):
message = html_text.extract_text(message)
except Exception as e:
print("an error has occurred")
# Removing the links
if "https" or "http" in message:
message = re.sub(r"http\S+", "", message)
# Removing special characters
for j in specialChars:
message = message.replace(j, "")
# Printing the message
print("Message: \n", message)
# Speak the data
engine = pyttsx3.init()
engine.say(
f"You got a mail From:{mail_from}. The mail is regarding:{mail_subject}.The mail contains:, {message}")
engine.runAndWait()
print("==========================================\n")
break
def check_mail(mail):
mail.select("INBOX")
# select specific mails
_, selected_mails = mail.search(None, 'UnSeen')
# total number of mails from specific user
unread_message = len(selected_mails[0].split())
print("Total Unread Messages:", unread_message)
if unread_message > 0:
get_mail(mail, selected_mails)
def login(email_id, pwd):
try:
mail = imaplib.IMAP4_SSL(gmail_host)
# login
mail.login(email_id, pwd)
check_mail(mail)
except Exception as e:
print("AN ERROR HAS OCCURRED:", e)
login(username, app_password)