-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailBot.py
More file actions
187 lines (145 loc) · 5.4 KB
/
EmailBot.py
File metadata and controls
187 lines (145 loc) · 5.4 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import smtplib # Simple Mail Transfer Protocol
import speech_recognition as sr
from email.message import EmailMessage
import pyttsx3 # Python Text to Speech version 3
import getpass # Take Hidden Password Input
import pyperclip # Copy text to clipboard
# Talk function which uses pyttsx3
def talk(text):
engine.say(text)
engine.runAndWait()
# Speech to text which uses Speech Recognition with google api
def getSpeech():
with sr.Microphone() as source:
print("listening...")
voice = listener.listen(source, phrase_time_limit=3)
try:
info = listener.recognize_google(voice)
return info
except:
return None
# Get email subject info
def subject():
talk("What is Subject of your Email?")
em_subject = getSpeech()
while em_subject == None:
talk("Can't understand, Speak Again!")
em_subject = getSpeech()
return em_subject
# Get email body info
def body():
talk("What is body of your Email?")
em_body = getSpeech()
while em_body == None:
talk("Can't understand, Speak Again!")
em_body = getSpeech()
return em_body
# Get sender's info
def sender():
talk("What is the name of sender?")
em_sender = getSpeech()
while em_sender == None:
talk("Can't understand, Speak Again!")
em_sender = getSpeech()
if em_sender.lower() in email_dict:
emailid = email_dict[em_sender.lower()]
else:
emailid = "Email ID Not Available"
return em_sender.title(), emailid
# Get receiver's info
def reciever():
talk("What is the name of reciever?")
em_reciever = getSpeech()
while em_reciever == None:
talk("Can't understand, Speak Again")
em_reciever = getSpeech()
if em_reciever.lower() in email_dict:
emailid = email_dict[em_reciever.lower()]
else:
emailid = "Email ID Not Available"
return em_reciever.title(), emailid
# Send Email using smtplib
def sendEmail(reciever, sender, subject, password, message):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender, password)
email = EmailMessage()
email['From'] = sender
email['To'] = reciever
email['Subject'] = subject
email.set_content(message)
server.send_message(email)
# Reading usernames and emails from text file
def readFile():
f = open("userEmails.txt", "r")
data = (f.read())
ls = []
x = ''
for i in data:
if i == '\n':
ls.append(x)
x = ''
else:
x = x+i
ls.append(x)
for i in ls:
name, email = i.split()
email_dict[name] = email
# Adding more usernames and emails to text file
def writeFile(name, email):
f = open("userEmails.txt", "a")
f.write(f"\n{name.lower()} {email.lower()}")
if __name__ == '__main__':
print("Email Sender launched\n")
email_dict = {}
readFile()
engine = pyttsx3.init()
listener = sr.Recognizer()
ans = 'yes'
while 'yes' in ans:
subject = subject()
body = body()
sender, s_email = sender()
reciever, r_email = reciever()
if s_email == "Email ID Not Available":
talk("Unable to fetch Email I D for the given sender, please type down sender's email")
s_email = input('>>')
writeFile(sender, s_email)
if r_email == "Email ID Not Available":
talk("Unable to fetch Email I D for the given reciever, please type down reciever's email")
r_email = input('>>')
writeFile(reciever, r_email)
print(f"\nTo: {reciever}({r_email})")
print("\nSubject:", subject)
print("\nBody:", body)
print("\nBest Regards,")
print(sender, "\n")
talk("Preview your Email, Do you want to send it?")
send = getSpeech()
while send == None:
talk("Can't understand, Speak Again")
send = getSpeech()
if 'yes' in send:
talk("Type down sender's password to send email.")
# password = getpass.getpass(prompt="Password: ")
password = input(">>")
try:
sendEmail(r_email, s_email, subject, password, body)
except:
talk("Invalid Credentials, your email have been copied to clipboard.")
pyperclip.copy(f"To: {reciever}({r_email})\nSubject: {subject}\nBody: {body}\n\nBest Regards,\n{sender}")
else:
talk("Do you want to copy this email to clipboard?")
copy = getSpeech()
while copy == None:
talk("Can't understand, Speak Again")
copy = getSpeech()
if 'yes' in copy:
pyperclip.copy(f"To: {reciever}({r_email})\nSubject: {subject}\nBody: {body}\n\nBest Regards,\n{sender}")
talk("Your Email have been successfully copied to clipboard")
talk("Do you want to send another Email?")
ans = getSpeech()
while ans == None:
talk("Can't understand, Speak Again")
ans = getSpeech()
ans = ans.lower()