Send Automated Emails using Rasa chatbot
Last Updated :
31 Aug, 2021
Rasa is a python module used to create custom AI chatbots. You can easily send automated emails to your users using the rasa chatbot. Rasa is a tool to build custom AI chatbots using Python and natural language understanding (NLU). Rasa provides a framework for developing AI chatbots that uses natural language understanding (NLU). It also allows the user to train the model and add custom actions.
In this article, we are going to see how to send emails using Rasa.
Flow Chart:

Creating a Rasa chatbot
First, you will need a rasa chatbot through which you can send automated emails.
To install the rasa module type the below command in the terminal (requires Python 3.6, 3.7, or 3.8).
pip3 install -U pip
pip3 install rasa
To create a new project with example training data type the below command in the terminal.
rasa init
Now your rasa chatbot is ready. You can talk to your chatbot using the below command.
rasa shell
Now, In the next step, we will add some more training data to this chatbot.
Adding Intents, Responses, and Stories
Now our chatbot can respond to basic user inputs, but we have to add more intents, responses, and stories to take email ID and Name from the user. For an in-depth explanation of intents, responses and stories refer to this article.
Adding Intents:
Here we need two new intents for name and email id. Add the below lines in your nlu.yml file.
- intent: email_id
examples: |
- [[email protected]](email)
- [abc@yourdomain](email)
- @gmail.com
- [[email protected]](email)
- intent: user_name
examples: |
- [YOURNAME](name)
- [RANDOM_PERSON_NAME](name)
Here we are also creating two slots ( email and name) in which we will store the user data so in your domain.yml file add the new slots and intents.

Adding Responses:
Now we have to add two new responses to our chatbot.
- To ask the user to enter name.
- To ask the user to enter an email id.
For this add the below lines in the responses section in the domain.yml file.
utter_askname:
- text: Please enter your name.
utter_askemail:
- text: Please enter email id to receive updates.

Adding Stories:
Now we have to add some relevant stories in our stories.yml file so remove all the stories in that file and add the below lines ( you can add more stories according to your need).
- story: GeekforGeek story
steps:
- intent: greet
- action: utter_askname
- intent: user_name
- slot_was_set:
- name: "YOURNAME"
- action: utter_askemail
- slot_was_set:
- email: '[email protected]'
- intent: email_id
- action: action_email
Here 'action_email' doesn't exist so we have to create this action to send emails but before that go to the endpoints.yml file and uncomment the lines below.
action_endpoint:
url: "http://localhost:5055/webhook"

Adding Custom Email Action
Now we will add a new class in our actions.py file to send automated emails. Add the below codes in the actions.py file.
Python3
# Importing required modules
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
import smtplib
# Creating new class to send emails.
class ActionEmail(Action):
def name(self) -> Text:
# Name of the action
return "action_email"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
# Getting the data stored in the
# slots and storing them in variables.
user_name = tracker.get_slot("name")
email_id = tracker.get_slot("email")
# Code to send email
# Creating connection using smtplib module
s = smtplib.SMTP('smtp.gmail.com',587)
# Making connection secured
s.starttls()
# Authentication
s.login("SENDER_EMAILID", "PASSWORD")
# Message to be sent
message = "Hello {} , This is a demo message".format(user_name)
# Sending the mail
s.sendmail("SENDER_EMAIL_ID",email_id, message)
# Closing the connection
s.quit()
# Confirmation message
dispatcher.utter_message(text="Email has been sent.")
return []
Explanation: First import rasa and smtplib module. Then creating a python class that will return an action named 'action_email' and get the username and email id from the slot and storing them in separate variables. Creating connection using SMTP method of smtplib module. And then Make the connection secured using the starttls() method of the smtplib module. And then Logging in to the Gmail account using the login() function that will take email id and password as parameters. Storing the message in a new variable named 'message'. And then send the Email using the Sendmail function that will take the sender's email id, receiver's email id, and message as its parameter. Then close the connection using the quit() function.
You also have to allow access to less secure apps in your Gmail account.
Log in to your Gmail account, and go to "Manage your Google Account".
Then click on the "Security" tab.
Then turn on the "Less secure App access":
After this add the below lines in the domain.yml file:
actions:
- action_email
Now everything is ready we just have to train our chatbot. For this type the below command is in the terminal:
rasa train
After training is complete you can talk to your chatbot by typing the below commands in the terminal. To run action server:
rasa run actions
To run the trained model:
rasa shell
Output:
Video Output:
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Machine Learning Tutorial
Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.It can
5 min read
Linear Regression in Machine learning
Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea
15+ min read
Support Vector Machine (SVM) Algorithm
Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. While it can handle regression problems, SVM is particularly well-suited for classification tasks. SVM aims to find the optimal hyperplane in an N-dimensional space to separate data
10 min read
K means Clustering â Introduction
K-Means Clustering is an Unsupervised Machine Learning algorithm which groups unlabeled dataset into different clusters. It is used to organize data into groups based on their similarity. Understanding K-means ClusteringFor example online store uses K-Means to group customers based on purchase frequ
4 min read
Logistic Regression in Machine Learning
In our previous discussion, we explored the fundamentals of machine learning and walked through a hands-on implementation of Linear Regression. Now, let's take a step forward and dive into one of the first and most widely used classification algorithms â Logistic RegressionWhat is Logistic Regressio
12 min read
K-Nearest Neighbor(KNN) Algorithm
K-Nearest Neighbors (KNN) is a supervised machine learning algorithm generally used for classification but can also be used for regression tasks. It works by finding the "k" closest data points (neighbors) to a given input and makesa predictions based on the majority class (for classification) or th
8 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
100+ Machine Learning Projects with Source Code [2025]
This article provides over 100 Machine Learning projects and ideas to provide hands-on experience for both beginners and professionals. Whether you're a student enhancing your resume or a professional advancing your career these projects offer practical insights into the world of Machine Learning an
5 min read
Naive Bayes Classifiers
Naive Bayes is a classification algorithm that uses probability to predict which category a data point belongs to, assuming that all features are unrelated. This article will give you an overview as well as more advanced use and implementation of Naive Bayes in machine learning. Illustration behind
7 min read