Sending email through action - Getting Started with Rasa / Tutorials, Resources & Videos - Rasa Community Forum

post by Youssef-0 on May 2, 2020

hey comm,

this is a solution on how to send emails through actions

#dont forget the other imports

import smtplib

class Sendmail(Action):

 def name(self) -> Text:
     return "action_send_mail"
  def run(self, dispatcher: CollectingDispatcher,
          tracker: Tracker,
         domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
         Subject = tracker.get_slot('Subject')
         Body = tracker.get_slot('Body')
         Recipient = tracker.get_slot('Recipient')

server = smtplib.SMTP_SSL('smtp.gmail.com', 465) #connect to smtp server
         server.login("sender@gmail.com", "sender_pswd")
         # you can write your mail and or get it from the slots with tracker.get_slot
         # if you are using your email is better that you dont pass it through the code for security u can
         # set path variables with email and password and use them instead

msg ="Subject: {} \n\n {} " .format(Subject,Body) #creating the message

server.sendmail(                     #send the email
         "sender@gmail.com",
         Recipient,
         msg)
         server.quit()

dispatcher.utter_message(" Email sended ! ")
         return [SlotSet("to",Recipient)] #just in my case, u can return ntg

post by Akhil1278 on May 27, 2020

can you share what should be written in domain.yml?

post by Youssef-0 on Jun 2, 2020

hey @Akhil1278

really sorry for my late repy,

and about my domain file i just define the entities and slots that we need in this case w need

entities : Subject, Body, Recipient

and slots with the same names of entities

and finally you need to mention the name action we are working on in action

and finally provide atleast a story to work with

am sorry but i lost the domain file of this example … let me know if you need smtg else hope that would help.

post by vishal4000 on Nov 13, 2020

@Youssef-0 can you please share code to send email through email

post by Youssef-0 on Nov 13, 2020

hey @vishal4000 i already shared the code take a look

post by maksudcs on Nov 16, 2020

please share the code along with domain, stories

post by Youssef-0 on Nov 29, 2020

hey @maksudcs really sorry for my late reply let me know if you still need help

post by nik202 on Feb 17, 2021

please share a complete domain, nlu, stories, rules, and actions files if you can. many thanks in advance.