Send Emails With Just a Voice Command Using Python

Send Emails With Just a Voice Command Using Python

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

Prerequisites

  • 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':'me@gmail.com','bill gates':'bill@gmail.com','elon musk':'elon@gmail.com'}

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):
    engine.say(audio)
    engine.runAndWait()

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.....")
       r.pause_threshold=1
      audio = r.listen(source)

    try:
        print("Recognizing.....")
        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('smtp.gmail.com',587) # create a SMTP object for connection with server

    server.ehlo()

    server.starttls() #TLS connection required by gmail

    server.login('sender@gmail.com','password')

    server.sendmail('sender@gmail.com',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:

            try:

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

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

                speak("what should i say")

                content = listen()

                to = dict[name]

                sendEmail(to,content)

                speak("email has been sent")

            except Exception as e:

                print(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!