NDepend Blog

Improve your .NET code quality with NDepend

Automate Writing Gmail Draft Replies with Google Apps Script calling ChatGPT API

August 10, 2025 3 minutes read

Automate Writing Gmail Draft Replies with Google Apps Script calling ChatGPT API

For nearly 30 years, professionals have spent countless hours replying to emails—a repetitive task that consumes valuable time. This article presents a simple, cheap, and effective way to generate email reply drafts, helping you save up to 90% of your keystrokes.

At our company, email is managed through Google Workspace. While AI features like Gemini’s smart reply tools are helpful they are limited to tiny responses. In 2025 we still can’t rely on Google to fully analyze our inbox, understand our workflows, and automatically draft relevant replies for every message.

The good news? You don’t have to wait. With Google Apps Script calling OpenAI API, you can already automate Gmail draft reply and streamline your email management today. It costs around a one to two USD cents per draft edited.

The Google Apps Script calling OpenAI API

Below is the skeleton of the Google Apps Script we use. TODO markers indicate where you should customize the code. Additional remarks follow after the script.

Remarks

Script features: We’ve developed common helper functions such as callOpenAI(prompt),  createThreadedReplyDraft(threadId, htmlBody, subject, to),  and hasDraftInThread(thread, drafts) to help you get up and running quickly.

Edit Draft vs Sending it: This approach focuses on drafting replies to save you time and keystrokes. While Google Apps Script can automatically send the edited draft, we believe it’s best practice to review and confirm that the response is exactly what you intend to send. Else, edit the response and re-adjust your prompts.

Determine the scenarios somehow and write a prompt per scenario: We found that building one large script to cover every possible scenario (pre-sales, leads, support, etc.) is not efficient. A better approach is to first identify which process or stage the email thread belongs to. This can be done by matching keywords in the subject or body of the email, or by making an initial call to the ChatGPT API with a dedicated determine-scenario-prompt followed by the email subject and body, or by combining both methods (keywords + API call).

You’ll need to spend some time fine-tuning your prompts. since the prompt will evolved, you will need to test for regressions. To do so create a Gmail label called autdrafttest and apply it to a set of incoming mock emails. To verify that all your prompts behave as expected, mark these threads as unread, update the line const selector = 'in:inbox is:unread -in:drafts label:autdrafttest' in your script, and then run it and check that drafted replies are correct.

Add Services: You will need to add some Services that your script can use like Gmail or Drive. For example, we use the excellent Gmail extension Simple Gmail Notes (SGN), which provides a thread-based note system. The notes are stored in our Google Drive, and our script can read and edit each note seamlessly. Doing so both add context to edit the reply and save some keystrokes. (Disclaimer: We are not affiliated with SGN).

Conclusion

With well-crafted refined prompts, our experience shows a significant time savings, allowing to handle over 90% of emails much faster—for example, increasing productivity from around 20 replies per hour to 60. The key is to iteratively test and refine your scripts and prompts whenever it’s beneficial to do so.

While there are many other approaches using AI agents and orchestrator solutions, like Zapier or Make we found this way to be particularly simple, cheap, effective and highly customizable.

We’re constantly hearing about new AI systems that outperform professionals in various fields. While we haven’t reached full replacement of human expertise—and hopefully won’t for some time—the real opportunity lies in combining AI capabilities with human skills to boost productivity.

Perhaps one day Google Gmail or Microsoft Outlook will offer an out-of-the-box solution that generates accurate draft replies based on your company’s email activity over the past X months. But will it be cheaper and more customizable than this approach? That remains to be seen.

Comments:

  1. I thought this was a .NET blog?

    1. Yes it is 99% .NET Blog, and sometime we post about more exotic findings worth sharing 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *