Coders Packet

Voice Email Bot using Python

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:-

  • Open your Gmail!
  • Go to Manage your Google Account.
  • Click on the Security
  • Scroll down a bit and click on Turn on access of Less secure app access setting.


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:-

  1. ‘listener’ :- Listener object of Recognizer() class of speech recognition module is made to recognize the voice input of the user.
  2. ‘engine’ :- This object of init() of pyttsx3 module is made to use further in the program to convert text into speech.
listener = sr.Recognizer()
engine = pyttsx3.init()


 Functions used in the code:-

  1. ‘def talk(text)’ :- This function will take the text as a parameter and let the system speak the text passed. After this runAndWait() function is invoked to block while processing all currently queued commands.
def talk(text):         


  1. ‘def get_info(lt)’ :- Parameter 'lt' stands for limit which is just a variable which will receive the value of seconds for which mic will receive voice. Exception handling is being used so that if user does not respond then program will be terminated. Every time this function is invoked it will print ‘I am all ears up…’ to tell the user that the system is now listening your response. The ‘voice’ variable will listen and store whatever user has spoken and then ‘info’ variable will store the interpreted text of the audio spoken by the user using Google API. It will print whatever stored in ‘info’ variable and then convert it in small letters.
def get_info(lt):       
        with sr.Microphone() as source:
            print('\nI am all ears up...')
            voice = listener.listen(source,phrase_time_limit=lt)
            info = listener.recognize_google(voice)
            return info.lower()
        talk("Sorry , Did not understand the audio.")


  1. ‘def send_email(receiver, subject, message)’ :- This function will first create SMPT server and then login into the server using the email id and password provided by the user in this function manually. This function has parameters to receive the receiver’s email address, subject and body of the text given by the user. After receiving the required details it will send the mail to the email address provided by the user.
def send_email(receiver, subject, message):         
    server = smtplib.SMTP('', 587)
    # Make sure to give app access in your Google account
    email = EmailMessage()
    email['From'] = 'user_email_id'
    email['To'] = receiver
    email['Subject'] = subject


  1. ‘def get_email_info()’ :- This function will first take the name of the receiver and then ask for his/her email address. After taking email it will ask for the confirmation and if the system will misinterpret the address it will ask the user to type it to be in the safe side and it will even apologise if it will misinterpret. After then it will ask for the subject and body of the mail and it will keep asking for the confirmation until the user will be satisfied. After getting user’s confirmation system will invoke the ‘send_email’ function and send the mail and ask the user if he/she wants to send more email or not.
def get_email_info():
    talk('To Whom you want to send email.')
    name = get_info(2)
    talk('What is the email address of '+name)
    talk('Is this the correct email of'+name+'?.... Ignore spaces')
    #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")
    #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
        talk(subject+'.......Is it the correct subject?')
        if 'yes' in sub_confirm:
            talk('My apologies for the mistake')
            talk('Please speak the subject again')
            subject = get_info(5)
    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
        talk(message+'.....Is this the correct text?')
        if 'yes' in text_confirm:
            talk('My apologies for the mistake')
            talk('Please speak the text of the body again')
            message = get_info(15)
    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:


Flow of the program :-

  • Email_bot will introduce himself by saying:-

         'Hi master, I am your email bot...'

  • get_email_info() function is invoked and inside it :
    • get_info() function is invoked many times whenever input is being taken.
    • send_email() function is also invoked after taking all required details to send the finalise mail.
talk('Hi master, I am your email bot...')
get_email_info() #Calling the function to start taking email content.


Download Complete Code


No comments yet