How to call send email agent with input from research agent

Hi Team,

How can I make one agent to send input i.e email body crafted in research agent sent to another agent. as of now

“my research agent has :At the end summarize the analysis and recommendation in {email_body} and dispatch it to email_agent with {email_body}”

and task looks like

task.yaml
send_email_task:
description: “Task to send an email to {from_email} with {email_body}”
agent: “email_agent”
inputs:
sender_email: “sendder”
sender_password: “pass”
recipient_email: “email”
subject: “Log updates”
body: {email_body}

agent.yaml
email_agent:
role: “Log Sentinels”
goal: “An agent to send automated emails.”
backstory:
agent max_retries is set to 1 with timeout of 60 sec

In crew.yaml
@task
def send_email_task(self) → Task:
return Task(
config=self.tasks_config[‘send_email_task’],
output_file=‘recommendation.md’,
tools=[tool(‘send_email_tool’)]
)

I am getting following error
pydantic_core._pydantic_core.ValidationError: 3 validation errors for Agent
role
Field required [type=missing, input_value={‘verbose’: True}, input_type=dict]
For further information visit Redirecting...
goal
Field required [type=missing, input_value={‘verbose’: True}, input_type=dict]
For further information visit Redirecting...
backstory
Field required [type=missing, input_value={‘verbose’: True}, input_type=dict]

Please provide some pointer how can I solve this.

Best,
Priti

Task and agent YAML files only accept certain inputs -

You will need to create a custom tool that allows you to interact with SMTP I.E Gmail

You should check out flows - Flows - CrewAI

Also we have an email example here - crewAI-examples/email_auto_responder_flow at main · crewAIInc/crewAI-examples · GitHub

To define a tool that sends emails

  1. Go to your custom_tools.py and add this into it

     from crewai.tools import BaseTool
     from typing import Type
     from pydantic import BaseModel, Field,ConfigDict
     from langchain_community.agent_toolkits import GmailToolkit
     from langchain_community.tools.gmail.create_draft import GmailCreateDraft
     from langchain_community.tools.gmail.send_message import GmailSendMessage'
    
     class CreateDraftTool(BaseTool):
         name: str = "Email Tool"
         description: str = (
             """
               Useful to create an email draft and send it too.
                 The input to this tool should be a pipe (|) separated text
                 of length 3 (three), representing who to send the email to,
                 the subject of the email and the actual message.
                 For example, "lorem@ipsum.com|Nice To Meet You|Hey it was great to meet you.".
                 and also send the email
             """
             )
         
         model_config = ConfigDict(arbitrary_types_allowed=True)
         def _run(self,argument:str) -> str:
             email, subject, message = argument.split("|")
             gmail = GmailToolkit()
             draft = GmailCreateDraft(api_resource=gmail.api_resource)
             draft2 = GmailSendMessage(api_resource=gmail.api_resource)
             result = draft({"to": [email], "subject": subject, "message": message})
             result2 = draft2({"to": [email], "subject": subject, "message": message})
             return f"\nDraft created: {result}\n Email Sent {result2}"    
    
  2. Go to your .env file and add
    MY_EMAIL = name.example@gmail.com

  3. Set Up Google Credentials
    1.Set Up Google Account: Follow the Google instructions to set up your Google account and obtain the credentials.json file.
    2. Download and Place credentials.json: Once you’ve downloaded the file, name it credentials.json and place it in the root of the project.

  4. In your crew.yaml go to the agent which is doing this task of sending emails and call the tool as follows
    tools = [CreateDraftTool()]

Thank you Sannidhya

I am stuck in error like
tool_functionstool for tool in tools
~~~~~~~~~~~~~~^^^^^^
KeyError: ‘SendEmail’

I have checked that all the agents , task and crew has correct name.

in crew.py , I am instantiating as below

from purple_crew.tools.email_tool import EmailTool
email_tool_instance = EmailTool()

but it is not working .

Please make sure you are using the latest crewai version and then if you are try using a decorators which goes @tool pretty similar to what is used to define agent and tasks like @agents and @task respectively.

But it would be really helpful if you could give me more context of what you are trying to do

Feel free to reach me out at
sannidhay2004@gmail.com if you don’t feel comfortable sharing your idea and your work in a public forum like this

thank you , I sent you an email with details