How to send email using smtplib in custom actions?

I am trying to send an email using smtplib. The code is:

def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

    loc = tracker.get_slot('location')
    colname=tracker.get_slot('col_name')
    print("==========",colname)

    db = pymysql.connect('localhost', 'root', 'Gandharv@4me', 'ashmita')
    cursor = db.cursor()
    
    fileName = tracker.get_slot('location')
    sql_query = "SELECT * FROM employees1 WHERE fname = %s "
    cursor.execute(sql_query, (fileName,))
    result=cursor.fetchall()
    for row in result:
        firstname=row[0]
        lastname=row[1]
        role=row[2]
    response= """ First Name : {}, Last Name: {}, Role : {} """.format(firstname,lastname,role)
    dispatcher.utter_message(response)
    print("--------------------------------",response)
    content="FirstName: "+firstname+"Lastname: "+lastname+"Role: "+role
    print("+++++++++++++++++++++++++",content)
    mail= smtplib.SMTP('smtp.gmail.com','587')
    mail.ehlo()
    mail.starttls()
    mail.login('myemail','mypas')
    mail.sendmail('pathakgandharv@gmail.com','pathakbabita00@gmail.com',content.as_string())
    mail.close()
    return [SlotSet('location',fileName),SlotSet('col_name',colname)]

Now, I would like to tell that it works fine if there is only a string in content. It works fine for content=firstname (firstname from the loop).I recieve the emails. Problem is I want the to send response in emails . But when I use mail.sendmail(‘pathakgandharv@gmail.com’,‘pathakbabita00@gmail.com’,response/content) it skips the code and the mail is not sent and it sometimes gives an exception ERROR asyncio - Task exception was never retrieved future: <Task finished coro=<configure_app..run_cmdline_io() done,

1 Like
  • I am not using content.as_string

Same is happening to me, any ideas?