How to Automate Draft Replies in Gmail with n8n

How to Automate Draft Replies in Gmail with n8n

Do you manage a lot of emails each day? Sometimes it can be difficult to keep up, especially if you struggle with what to write. We can help with that! In this tutorial, we will show you how to use n8n to create a workflow that automatically generates draft replies in Gmail. This way, you remain in control, editing and approving emails before they are sent out.

Step-by-Step Guide

Step 1: Add Gmail Trigger

  1. Go to n8n and open your workflow editor.
  2. Add a new node and choose the "Gmail Trigger" node.
  3. Configure it to trigger every minute to check for new emails. Only emails that are not from you (using the query -from:me) will be processed.
gmail watch emails

Step 2: Assess If the Email Needs a Reply

  1. Add an "OpenAI Chat" node and configure it to use the GPT-4o model.
  2. Add the "JSON Parser" node. We will use a simple JSON schema to determine if a response is needed.
{
  "type": "object",
  "properties": {
    "needsReply": {
      "type": "boolean"
    }
  },
  "required": ["needsReply"]
}
  1. Connect the "Gmail Trigger" node to the "Assess if message needs a reply" node. This will process new emails and decide if they need a response based on our criteria.
asses if message needs reply

Step 3: Setup Condition to Check if Reply is Needed

  1. Add an "If" node to check if the email needs a reply.
    • Set the condition to check if {{ $json.needsReply }} is true.
  2. Connect this "If" node to the output of the "Assess if message needs a reply" node.
if needs reply module

Step 4: Generate the Email Reply

  1. If the email needs a reply, add a "Generate email reply" node.
    • Use OpenAI GPT-4o to draft the reply.
    • Set up a prompt to generate a professional reply. Here’s an example prompt:
Subject: {{ $('Gmail Trigger').item.json.subject }}
Message: {{ $('Gmail Trigger').item.json.textAsHtml }}
  1. Configure the context of the email reply. Here’s a suggestion for the prompt context:
You're a helpful personal assistant and your task is to draft replies on my behalf to my incoming emails. Ensure that the reply is suitable for a professional email setting and addresses the topic in a clear, structured, and detailed manner. Do not make things up. Here are some guidelines: 
- Be concise and maintain a business casual tone.
- Start with "Hello," and end with "Best,"
- When replying to yes-no questions, draft 2 responses: one affirmative and one negative separated by " - - - - - - - OR - - - - - - - ".
- If you don't know an answer, leave placeholders like "[YOUR_ANSWER_HERE]".
- Don't use any special formatting, only plain text.
- Reply in the same language as the inbound email.
openai to generate reply

Step 5: Create the Draft in Gmail

  1. Add a "Gmail - Create Draft" node.
    • Set the subject to Re: {{ $('Gmail Trigger').item.json.headers.subject }}.
    • Convert the generated plain text reply into HTML using {{ $json.text.replace(/\\n/g, "<br />\\n") }}.
    • Configure Gmail to create a draft message and link it to the correct email thread.
  2. Connect the output of the "Generate email reply" node to this "Gmail - Create Draft" node.
gmail draft module

Bringing It All Together

  1. Ensure all the nodes are connected properly:
    • "Gmail Trigger" -> "Assess if message needs a reply" -> "If Needs Reply" -> "Generate email reply" -> "Gmail - Create Draft".
  2. Save and activate your workflow.

Now, your n8n workflow will automatically check for new emails, assess if they need a reply, generate a draft, and place it in your Gmail, awaiting your final touch! 😎

Need More Help?

Feel free to reach out if you have any questions or need assistance setting up your workflows. You can contact us via email or find us on Upwork.

Happy automating! 🚀

Read more