In this tutorial, you'll learn how to create an AI agent that can send emails on your behalf. We'll use Microsoft Copilot Studio and the robust infrastructure of Azure Communication Services to create the AI agent that can be published on M365 Copilot, Teams, the web and many more.
You can view the video tutorial for creating your AI agent below:
Prerequisites:
- Microsoft Copilot Studio Access: You will need a license to use the platform.
- Azure Subscription: An active Azure account is required to set up the email sending service.
Step 1: Create Your First Agent in Copilot Studio
First, we'll create the basic framework for our AI assistant.
- Navigate to Copilot Studio: Open your web browser and go to copilotstudio.microsoft.com.
- Describe Your Agent: On the homepage, you'll see a prompt that says, "Describe your agent to create it." In the text box, type a simple description of what you want your agent to do. For this tutorial, use: "I want to create an agent that can send out emails using Azure communication services."
- Configure the Agent: Copilot Studio will generate a basic agent for you.
- You'll be taken to a configuration page. Give your agent a descriptive name, such as "AI Email Agent."
- Review the auto-generated description and instructions. You can modify these later if needed.
- Create the Agent: Click the Create button. Copilot Studio will now set up your agent's environment.
Step 2: Set Up Your Email Service in Azure
To give your agent the ability to send emails, we need to configure a backend service in Microsoft Azure.
- Go to the Azure Portal: Log in to your account at portal.azure.com.
- Create Resources: You will need to create two services:
- Communication Services: Search for "Communication Services" in the marketplace and create a new resource. This will serve as the main hub. More details can be found here: Create a communication service resource.
- Email Communication Services: Search for "Email Communication Services" and create this resource. This service is specifically for adding email capabilities. More details can be found here: Send email via azure communication services email
- Get Your Sender Address: Once these resources are created, you will have a sender address (e.g., DoNotReply@...). This is the email address your AI agent will use.
Step 3: Connect Your Services with an Azure Function
We need a way for Copilot Studio to communicate with Azure Communication Services to send the email. An Azure Function is the perfect tool to act as this bridge.
- Create a Function App: In the Azure portal, create a new Function App. This app will host our code. When setting it up, select Python as the runtime stack.
- Develop the Function: You'll write a simple Python script that acts as a web API. This script will:
- Receive data (recipient, subject, body) from Copilot Studio.
- Connect to your Azure Communication Services resource.
- Send the emai
- The code for the Azure Function app can be found on our Github page
- Deploy the Function: Once your code is ready, deploy it to the Function App you created. After deployment, make sure to get the Function URL, as you'll need it in the next step.
- In the Azure portal head over to your Function App. Go to Settings > Environment Variables, click Add. Add the following:
- Name: ACS_CONNECTION_STRING
- Value: <insert your connection string>
- Name: ACS_SENDER_EMAIL
- Value: <insert your send email address> (e.g. “DoNotReply@...)
- Make sure to click Apply, so your settings are applied.
Note: For this tutorial, you can use Visual Studio Code with the "Azure Functions" and "Azure Resources" extensions installed to write and deploy your code efficiently.
Step 4: Design the Conversation Flow in Copilot Studio
Now, let's teach our agent how to interact with users to gather the necessary email details. We'll do this using a "Topic."
- Go to the Topics Tab: In your Copilot Studio agent, navigate to the Topics tab on the menu.
- Create a New Topic: Click + Add a topic and select From blank. You can ask Copilot to create a topic for you but for this tutorial we are going to create from scratch.
- Set the Trigger: The first node is the Trigger. This is what starts the conversation.
- Click on it and under "Phrases," add phrases like "send an email." This means whenever a user types this phrase, this topic will activate.
- Ask for the Recipient:
- Click the + icon below the trigger and select Ask a question.
- In the text box, type: "What's the recipient email?"
- Under "Identify," choose User's entire response.
- Save the response in a new variable called varRecipient.
- Ask for Subject and Body: Repeat the process above to ask for the Subject and Message body. Save the responses in variables named varSubject and varBody, respectively.
- Ask for Personalization (Optional): You can also ask for the Recipient's name for a personalized greeting and save it in a variable called varName.
Step 5: Call the Azure Function to Send the Email
With all the information gathered, it's time to send it to our Azure Function.
- Add an HTTP Request Node: Click the + icon below your last question. Go to Advanced > Send HTTP request.
- Configure the Request:
- URL: Paste the Function URL you copied from your Azure Function.
- Method: Select POST.
- Headers and body: Click Edit.
- For the body, select JSON content and use the formula editor to structure your data, mapping your topic variables (e.g., Topic.varSubject, Topic.varBody) to the JSON fields your function expects.
- Save the Response: The result from the Azure Function can be saved to a variable, such as varSendResult. This allows you to check if the email was sent successfully.
Step 6: Test and Publish Your Agent
Your AI email assistant is now fully configured! Let's test it out.
- Open the Test Panel: On the right side of the screen, open the Test your agent panel.
- Start a Conversation: Type your trigger phrase, "send an email," and press Enter.
- Follow the Prompts: The agent will ask you for the recipient, subject, and body. Provide the information as requested.
- Check Your Inbox: The agent will process the request and send the email. Check the recipient's inbox to confirm it has arrived!
- Publish: Once you are satisfied with the agent's performance, click the Publish button at the top of the page. You can then go to the Channels tab to deploy your agent to various platforms like Microsoft Teams, a demo website, or even Facebook.
Congratulations! You have successfully built and deployed a functional AI email agent.
Debugging and Troubleshooting
Agent asking unnecessary information: If you want the agent to only ask the question that you have input and to not add anything extra. In your agent settings in Copilot Studio, turn off generative AI orchestration. This will force the AI agent to only asks questions
Agent stuck in a loop: If, after sending, the agent jumps back to “What’s the recipient email?”, explicitly end the topic. Add + → Topic management → End current topic immediately after your success message. If you want branching, remember the HTTP Request action has no built-in success check, you can save the response (e.g., varSendResult), add a Condition that tests varSendResult.ok == true, and on success send a confirmation and End current topic to prevent loops.