How I Made AI Assistants Answer My Emails: n8n Tutorial
Science & Technology
Introduction
In this article, I want to share how I've automated my email responses, particularly for sponsorship opportunities, by leveraging AI agents through n8n. The goal of this workflow is not just to respond automatically to emails but also to extract valuable information, draft responses based on predefined terms, and save them for review before sending.
Overview of Our Workflow
Initially, I started by creating a workflow that triggers when I receive an email in my Gmail account. The first step involved setting up n8n, a free tool for building automation workflows. I then added the Gmail node that triggers the workflow upon receiving new messages.
Step 1: Setting Up Gmail Node
- Create a New Workflow: Start by deleting the default trigger.
- Add Gmail Node: Search for the Gmail node and select the "on message received" trigger.
- Create New Credentials: You will need a client ID and client secret from Google’s Cloud Console.
To set these up:
- Go to cloud.google.com, create a new project, enable the Gmail API, and set up the OAuth consent screen.
- Create OAuth credentials and add your authorized redirect URI from n8n.
- Copy the client ID and client secret to n8n and authenticate the connection.
Step 2: Fetching New Emails
With the trigger connected, I configured n8n to check for new emails every minute. I started testing the connection with security alerts from Google, then moved onto fetching the actual sponsorship emails.
Step 3: Extract Email Context
To create meaningful interactions with the AI agent, I extracted essential details from the emails, such as the sender's email address, subject, and body. This was performed using a Set node to format all this information into a single variable called "email context".
Step 4: AI Agent for Determining Sponsorship
Using the AI capabilities, I added an AI agent node to evaluate whether the received email was a sponsorship opportunity. Here's a brief overview of how the AI agent works:
- Configure the prompt for the AI agent to analyze the email context and determine if it is related to sponsorship.
- Return a structured output containing a Boolean value and a reasoning string to explain the decision.
Step 5: Handling Email Response
- If Node: I implemented an if node to check the Boolean value from the AI output. If the email is relevant to sponsorship, we proceed; otherwise, we do nothing.
- Storing in Airtable: I connected the Airtable node to store information about the lead.
For this:
- Set up an Airtable base and define necessary fields such as lead name, email address, company, and reasoning.
- Implement the logic to extract these details from the email context and push them into Airtable.
Step 6: Drafting the Response Email
With the lead information extracted and stored, I used a different OpenAI node to formulate a professional response email based on my channel's terms and conditions.
- Adjust the system prompt to instruct the AI on how to respond while including important details about my channel.
- Finally, I had the option to reply to the email immediately or save the response as a draft for later review.
Step 7: Sending or Saving Draft Emails
To complete the workflow:
- For immediate responses, configure the Gmail node to reply directly.
- For saving drafts, switch the action to create a draft and include the sender's email in the options.
After several iterations for adjusting formatting, ensuring the proper sending of emails, and managing drafts, the workflow was complete.
Conclusion
This tutorial highlights how easy it can be to automate email responses using tools like n8n and OpenAI. By following these steps, users can efficiently manage inbound sponsorship inquiries and free up time for other tasks.
Keywords
- AI agent
- n8n
- Email automation
- Gmail API
- Airtable
- Sponsorship opportunities
- OpenAI
- Automation workflow
FAQ
Q1: What is n8n?
A1: n8n is an open-source workflow automation tool that enables you to automate tasks between different applications without needing to code.
Q2: How do I connect Gmail with n8n?
A2: You connect Gmail to n8n by creating a project in Google Cloud, enabling the Gmail API, and obtaining OAuth credentials to authenticate.
Q3: Can I modify the email draft before sending?
A3: Yes, you can save the email response as a draft in n8n, allowing you to approve it before sending it out.
Q4: How does the AI determine if an email is a sponsorship opportunity?
A4: The AI evaluates the content of the email based on predefined prompts and responds with a Boolean value indicating sponsorship relevance.
Q5: What is Airtable and how is it used in this automation?
A5: Airtable is a cloud-based platform for organizing and managing data. In this workflow, it's used to store information about potential sponsors from emails.