-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.py
190 lines (134 loc) · 5.14 KB
/
driver.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
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
188
189
190
# Python code to listen and send the mail to receiver
import speech_recognition as sr
import pyttsx3
from pyttsx3 import voice
import time
import smtplib
import getpass as gp
# Initialize the recognizer
r = sr.Recognizer()
# Function to convert text to speech
def SpeakText(command):
# Initialize the engine
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()
# Function to send the mail
def Sendmail(server, port, xsender, xpassword, xreceiver, xmessage):
# Sending mail
# Creates SMTP session
s = smtplib.SMTP(server, port)
s.ehlo()
# start TLS for security
s.starttls()
# Authentication
s.login(str(xsender), str(xpassword))
# sending the mail
s.sendmail(str(xsender), str(xreceiver), str(xmessage))
# terminating the session
s.quit()
# Prompt to the user
print("\nThe mail was sent successfully...")
SpeakText("Mail Sent successfully, Thank You for using this program")
# Function to display an error message when the mail is not sent
def ShowError():
print("\nWe are having problem sending your mail.");
SpeakText("We are having problem sending your mail.")
time.sleep(1)
print("\nPlease check your mail and authenticate the program and enable less secure apps for your account.")
SpeakText("Please check your mail and authenticate the program and enable less secure apps for your account.")
time.sleep(1)
print("\nHope it helps, Thank You")
SpeakText("Hope it helps, Thank You")
# Taking information from the user about the mail
print("\n\nHello! Welcome to Voice Based Mailing Service")
SpeakText("Hello! Welcome to Voice Based Mailing Service")
time.sleep(0.5)
print("You need to login first, so we need your Email and Password...")
SpeakText("You need to login first, so we need your email and password")
time.sleep(1)
# Enter the credentials
SpeakText("\nPlease Enter your Email")
sender = input("\nEmail : ")
SpeakText("\nAlright! Now enter your password")
password = gp.getpass()
time.sleep(1)
# Initialize subject and body, helpful if any error pop from speech recognizer
subject = 'no'
body = 'no'
# Get the message from the user
# Exception handling to handle
# exceptions at the runtime
try:
# use the microphone as source for input.
with sr.Microphone() as source2:
# wait for a second to let the recognizer
# adjust the energy threshold based on
# the surrounding noise level
r.adjust_for_ambient_noise(source2, duration=0.2)
#listens for the user's input
SpeakText("You are good to go.");
time.sleep(1)
SpeakText("Tell me the subject.")
print("\nSubject : Listening...")
audio2 = r.listen(source2)
# Using google to recognize audio
subject = r.recognize_google(audio2)
subject = subject.lower()
print("\nYour Subject: " + subject)
SpeakText("Tell me the message body.")
print("\nBody : Listening...")
audio2 = r.listen(source2)
# Using google to recognize audio
body = r.recognize_google(audio2)
body = body.lower()
print("Your Body: \n" + body)
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
if subject == 'no':
subject = "Ignore this mail..."
if body == 'no':
body = "Sorry :( Sent by-mistake..."
except sr.UnknownValueError:
print("unknown error occured")
if subject == 'no':
subject = "Ignore this mail..."
if body == 'no':
body = "Sorry :( Sent by-mistake..."
# Get the sender details
SpeakText("Well Done! Please enter receiver EMail Address.")
receiver = input("\nReceiver's Email: ")
# Add the subject and body to complete the message
message = 'Subject: ' + subject + "\nDear " + receiver + ', \n\n' + body + '\nSent from: https://github.com/subhamsagar524/Voice-Based-Mailing-from-Terminal'
# Prompt the user to choose the service
SpeakText("Now Please choose your mailing service.")
choose = int(input("\n\n1. Gmail\n2. Yahoo\n3. Outlook\nChoose your server : "))
SpeakText("Ok, we are sending your mail.")
SpeakText("You will be informed once the mail is sent.")
# Try to send the EMail through different service
if choose == 1:
# Try sending the mail through Gmail
try:
Sendmail('smtp.gmail.com', 587, sender, password, receiver, message)
# If not sent inform the user
except:
ShowError()
elif choose == 2:
# Try sending the mail through Yahoo
try:
Sendmail('smtp.mail.yahoo.com', 587, sender, password, receiver, message)
# If not sent inform the user
except:
ShowError()
elif choose == 3:
# Try sending the mail through Outlook
try:
Sendmail('smtp-mail.outlook.com', 587, sender, password, receiver, message)
# If not sent inform the user
except:
ShowError()
else:
print("\nInvalid Option\nThank You...")
SpeakText("We do not support any other services.")
time.sleep(1)
SpeakText("Thanks for using the Program.")