# Sending email through action

**URL:** <https://forum.rasa.com/t/sending-email-through-action/28328>\
**Category:** Tutorials, Resources & Videos\
**Created:** [May 2, 2020, 3:12pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328 "2020-05-02T15:12:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Youssef-0](https://avatars.discourse-cdn.com/v4/letter/y/a6a055/32.png) [@Youssef-0](https://forum.rasa.com/u/Youssef-0)\
**Post date:** [May 2, 2020, 3:12pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328/1 "2020-05-02T15:12:58Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Akhil1278](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Akhil1278](https://forum.rasa.com/u/Akhil1278)\
**Post date:** [May 27, 2020, 2:00pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328/2 "2020-05-27T14:00:57Z")

</div>

> [@Youssef-0](#):
>
> import smtplib

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

---

<div class="post-metadata">

**Author:** ![Akhil1278](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@Akhil1278](https://forum.rasa.com/u/Akhil1278)\
**Post date:** [May 27, 2020, 2:01pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328/3 "2020-05-27T14:01:11Z")

</div>

@Youssef-0

---

<div class="post-metadata">

**Author:** ![Youssef-0](https://avatars.discourse-cdn.com/v4/letter/y/a6a055/32.png) [@Youssef-0](https://forum.rasa.com/u/Youssef-0)\
**Post date:** [June 2, 2020, 7:40pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328/4 "2020-06-02T19:40:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vishal4000](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/vishal4000/32/11814_2.png) [@vishal4000](https://forum.rasa.com/u/vishal4000)\
**Post date:** [November 13, 2020, 12:07pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328/5 "2020-11-13T12:07:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Youssef-0](https://avatars.discourse-cdn.com/v4/letter/y/a6a055/32.png) [@Youssef-0](https://forum.rasa.com/u/Youssef-0)\
**Post date:** [November 13, 2020, 12:58pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328/6 "2020-11-13T12:58:40Z")

</div>

hey @vishal4000 i already shared the code take a look 👆 👆

---

<div class="post-metadata">

**Author:** ![maksudcs](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@maksudcs](https://forum.rasa.com/u/maksudcs)\
**Post date:** [November 17, 2020, 4:17am UTC](https://forum.rasa.com/t/sending-email-through-action/28328/7 "2020-11-17T04:17:17Z")

</div>

> [@Youssef-0](#):
>
> `get_slot`

please share the code along with domain, stories

---

<div class="post-metadata">

**Author:** ![Youssef-0](https://avatars.discourse-cdn.com/v4/letter/y/a6a055/32.png) [@Youssef-0](https://forum.rasa.com/u/Youssef-0)\
**Post date:** [November 29, 2020, 8:21pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328/8 "2020-11-29T20:21:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [February 17, 2021, 6:02pm UTC](https://forum.rasa.com/t/sending-email-through-action/28328/9 "2021-02-17T18:02:30Z")

</div>

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