0
import speech_recognition as sp
import smtplib 
import pyttsx3
from email.message import EmailMessage

listener = sp.Recognizer()
speak = pyttsx3.init()

def talk(text) : 
    speak.say(text)
    speak.runAndWait() 

def command ():
    try:
        with sp.Microphone() as source :
            print("listening ...")
            voice = listener.listen(source)
            text = listener.recognize_google(voice)
            print(text)
            return text.lower()
    except:
        pass

def send(receiver,subject,body):
    server = smtplib.SMTP("smtp.gmail.com", 587) 
    server.starttls() 
    server.login("asbedi2004@gmail.com","xxxxxxxx")
    email = EmailMessage()
    email["From "] = "asbedi2004@gmail.com"
    email["To"] = receiver
    email["Subject"] = subject
    email.set_content(body)
    server.send_message(email)



email_list = {"angad" : "angadsb4069@gmail.com"}




def email_info():
    talk("Who do you want to send the email to ?")
    recipient = command()
    receiver = email_list[recipient]
    talk("What is the subject of the email?")
    subject = command()
    talk("What is the body of the email?")
    body = command()
    send(receiver , subject , body)
    talk("Hey you good for nothing lazy , your email has been sent")
    talk("Do you want to send another email")
    send_again = command()
    if "yes" in send_again:
        email_info()


email_info()

i have allowed the less secure app access on the gmail meanage setting. I have also checked the email id and the password, they are correct!

here is the error:

listening ... Angad listening ... code not working listening ... someone please help me with this code it is not working it shows this error

Traceback (most recent call last):
  File "/Users/angadsinghbedi/Desktop/PycharmProject/email_bot/email_bot.py", line 58, in <module>
    email_info()
  File "/Users/angadsinghbedi/Desktop/PycharmProject/email_bot/email_bot.py", line 50, in email_info
    send(receiver , subject , body)
  File "/Users/angadsinghbedi/Desktop/PycharmProject/email_bot/email_bot.py", line 33, in send
    server.send_message(email)
  File "/Users/angadsinghbedi/.pyenv/versions/3.6.0/lib/python3.6/smtplib.py", line 947, in send_message
    ''.join([from_addr, *to_addrs]).encode('ascii')
TypeError: sequence item 0: expected str instance, NoneType found

  
Tobin
  • 1,481
  • 1
  • 8
  • 16
  • What arguments are being passed to `send()`? – kluvin Jan 04 '21 at 07:56
  • send has 3 arguments - the receiver the subject and the body. I had to do this because in the email_info() function i am calling the send function which will send the mail to the receiver . Then i incorporated the same logic in the send() function by importing EmailMessage so that i dont need to manually change the values everytime i want to send a mail. – angadsinghbedi Jan 04 '21 at 08:01
  • And I can see those values are coming from `command`. But what are those values precisely? – kluvin Jan 04 '21 at 09:48

1 Answers1

0

Error clearly shows that its a TypeError. So if you look into smtplib.py in your library destination directory, on line 947 its clear that it is joining the parameters passed to a function send_message(). But the key thing here is: from_addr=None, to_addrs=None this parameters are set by default to None till they are specified again by user. So when join is used on line 947 it appears that it is joining two different datatypes which are string and None which resulted in a TypeError

So Here long story short when you call server.send_message(email) some how email object dose not contain the required parameters which has to be overwritten(from_addr,to_addrs).

So try to print contents of email to see what is missing. You can also checkout https://stackoverflow.com/a/17596848/11359097 this link for alternate method to send email.

Many other things should also be taken into consideration here, such as tls encryption, 2 Step Verification. Also you need to quit server as well, you can do this by using server.quit()

  • hey, sahil... i don't know what happened but I just re typed the send() function and it worked.... thanks for all that bro. ill definitely incorporate quitting it and will read more about tls and 2 step and how to implement it . – angadsinghbedi Jan 04 '21 at 11:21