How to Automate Draft Replies in Gmail with n8n
![How to Automate Draft Replies in Gmail with n8n](/content/images/size/w1200/2024/06/Screenshot-2024-06-11-140406.png)
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
- Go to n8n and open your workflow editor.
- Add a new node and choose the "Gmail Trigger" node.
- 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](https://blog.thinkbot.agency/content/images/2024/06/image-38.png)
Step 2: Assess If the Email Needs a Reply
- Add an "OpenAI Chat" node and configure it to use the GPT-4o model.
- 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"]
}
- 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](https://blog.thinkbot.agency/content/images/2024/06/image-37.png)
Step 3: Setup Condition to Check if Reply is Needed
- Add an "If" node to check if the email needs a reply.
- Set the condition to check if
{{ $json.needsReply }}
istrue
.
- Set the condition to check if
- Connect this "If" node to the output of the "Assess if message needs a reply" node.
![if needs reply module](https://blog.thinkbot.agency/content/images/2024/06/image-36.png)
Step 4: Generate the Email Reply
- 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 }}
- 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](https://blog.thinkbot.agency/content/images/2024/06/image-35.png)
Step 5: Create the Draft in Gmail
- 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.
- Set the subject to
- Connect the output of the "Generate email reply" node to this "Gmail - Create Draft" node.
![gmail draft module](https://blog.thinkbot.agency/content/images/2024/06/image-34.png)
Bringing It All Together
- Ensure all the nodes are connected properly:
- "Gmail Trigger" -> "Assess if message needs a reply" -> "If Needs Reply" -> "Generate email reply" -> "Gmail - Create Draft".
- 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! 🚀