By Faizan Ahmad
Voice email bot is developed to send emails to anyone by just speaking. System will ask for required information via voice and then send the mail to the mentioned email address in text format.
Access needs to be given:-
Google will not allow external server to send a Gmail and hence this program will give authentication error. We must enable the option from Gmail account setting to get the permission for the server to send Gmail. Below are the steps to enable that option:-
Modules being used:-
1. 'import smptlb' :- It stands for Simple Mail Transfer Protocol, is a protocol, which handles sending e-mail and routing e-mail between mail servers.This module is pre installed if you have any IDE of Python installed in your system.
2. 'import speech_recognition' :-It performs speech recognition, with support for several engines and APIs, online and offline. To install this module you just need to copy and paste the below code in terminal or command prompt.
pip install SpeechRecognition
3. 'import pyttsx3' :- It is a text-to-speech conversion library in Python and it also work offline. Installation syntax is given below:-
pip install pyttsx3
4. 'PyAudio' :- We can easily use Python to play and record audio by installing this module. Syntax is given below to install :-
pip install PyAudio
5. 'from time import sleep' :- It is used to suspend the execution of the program for specified time. It is already present in Python.
6. 'from email.message import EmailMessage' :- It is an email module and used to tell the compiler that we are structuring an email. It is already present in Python.
import smtplib import speech_recognition as sr import pyttsx3 from time import sleep from email.message import EmailMessage
Objects being made:-
listener = sr.Recognizer() engine = pyttsx3.init()
Functions used in the code:-
def talk(text): engine.say(text) engine.runAndWait()
def get_info(lt): try: with sr.Microphone() as source: print('\nI am all ears up...') voice = listener.listen(source,phrase_time_limit=lt) info = listener.recognize_google(voice) print(info) return info.lower() except: talk("Sorry , Did not understand the audio.") pass
def send_email(receiver, subject, message): server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() # Make sure to give app access in your Google account server.login('user_email_id','user_email_password') email = EmailMessage() email['From'] = 'user_email_id' email['To'] = receiver email['Subject'] = subject email.set_content(message) server.send_message(email)
def get_email_info(): temp=0 temp2=0 talk('To Whom you want to send email.') name = get_info(2) sleep(0.5) talk('What is the email address of '+name) receiver=get_info(7) talk('Is this the correct email of'+name+'?.... Ignore spaces') sleep(1) confirm=get_info(1) #System will ask you for confirmation if there is any misinterpretation then it will ask the address again if 'no' in confirm: talk('My apologies...It would be better if you type it...') receiver=input("Type the email address of "+name+" please...\n") else: pass #Deleting extra spaces,taken by the system, from the email address receiver=receiver.replace(" ","") print(name+"'s email address is : "+receiver) talk('What is the subject of your email that you want to send ?') subject = get_info(5) #System will ask you for confirmation if there is any misinterpretation then it will ask the subject again while(temp==0): talk(subject+'.......Is it the correct subject?') sleep(1) sub_confirm=get_info(2) if 'yes' in sub_confirm: temp=1 else: talk('My apologies for the mistake') talk('Please speak the subject again') subject = get_info(5) temp=0 talk('Tell me the text of the body of your email') message = get_info(10) #System will ask you for confirmation if there is any misinterpretation then it will ask the text again while(temp2==0): talk(message+'.....Is this the correct text?') sleep(2) text_confirm=get_info(2) if 'yes' in text_confirm: temp2=1 else: talk('My apologies for the mistake') talk('Please speak the text of the body again') message = get_info(15) temp2=0 send_email(receiver, subject, message) talk('Hey Master....Your email is sent') #It is important to ask the user if he/she wants to send more email or not talk('Do you want to send more email apart from '+name+'?') send_more = get_info(2) if 'yes' in send_more: get_email_info() else: pass
Flow of the program :-
'Hi master, I am your email bot...'
talk('Hi master, I am your email bot...') get_email_info() #Calling the function to start taking email content.
Submitted by Faizan Ahmad (fpawn123)
Download packets of source code on Coders Packet