Send Emails With Just a Voice Command Using Python

Mr. Unity Buddy's photo
Mr. Unity Buddy
·Sep 17, 2021·

3 min read

Send Emails With Just a Voice Command Using Python
Play this article

Hello, buddies! Most developers have one thing in common— Laziness. If you also belong to that category, I bet that you don't like writing and replying to emails all day. But we have one quick solution— writing a python program to do that task using your voice.

If you're a lazy person like me, you might not like writing and replying to emails 🙄🙄 So here's a quick solution, just use Python + Your Mic

This article will show how to send and check emails by giving voice commands. The Gmail API lets you view and manage Gmail mailbox data like threads, messages, and labels. More information about Gmail API can be found here


  • Basic Python Knowledge

  • And 3 libraries.

import pyttsx3

import speech_recognition as sr

import smtplib

Coding Time!

Let's begin!

First of all, we have to add people and their email addresses. You can add may as you want. Use a dictionary for that!

dict = {'me':'','bill gates':'','elon musk':''}

We should initialize the speech engine using a driver so that it can convert text to speech. For windows, sapi5 driver is used. Our speech engine requires a voice to be set. So get the information of voices available using getProperty() and choose a voice and set the property to the engine using setProperty().

engine = pyttsx3.init('sapi5') 
voices = engine.getProperty('voices') # getting different voices
engine.setProperty('voice',voices[0].id) # first voice chosen(male voice)

Then create a function 'speak' where the program speaks to the user. It takes a string as an argument and that string will be spoken by the program.

def speak(audio):

Create a listen function that listens to the user as well.

def listen(): 

    #it takes microphone input from the user and returns string output

    r = sr.Recognizer()

    with sr.Microphone() as source:
        print("Iam listening.....")
      audio = r.listen(source)

        query = r.recognize_google(audio, language = 'en-in') # setting language
        print(f"User said:{query}\n")

  except Exception as e:

       speak("Say that again please...")
       return "None"

    return(query) # user's speech returned as string

Next, we have to write a function that sends emails.

def sendEmail(to,content):

    server = smtplib.SMTP('',587) # create a SMTP object for connection with server


    server.starttls() #TLS connection required by gmail


    server.sendmail('',to,content) # from, to, content

Now coming to the main function. Users can say ' send email to <receiver_name> ' to send emails to the receiver 📧

f __name__ == "__main__":

    while(True):  # run infinite loop

        query = listen().lower()

        if 'email to' in query:


                name = list(query.split()) # extract receiver's name

                name = name[name.index('to')+1]

                speak("what should i say")

                content = listen()

                to = dict[name]


                speak("email has been sent")

            except Exception as e:


                speak("sorry unable to send the email at the moment.Try again")

You're done! 🍾

That's all about it! It's quite easy, now you don't have to spend more time writing emails 🙌🙌

Get full code

Thanks for reading and Happy Coding!

Share this