Building a personalized AI-powered mail merge in Google Apps Script with Gemini

Recently, I had a blast working with Gemini to bring a fun idea to life: creating a Google Apps Script that uses AI to generate personalized content for a mail merge. What started as a search for a harmless prank evolved into a fun exploration of AI’s creative potential. As someone who collaborates with technical leads and change management experts to oversee successful Google Workspace deployments, I’m always looking for innovative ways to leverage these tools. This little experiment with Gemini was no exception, demonstrating just how quickly innovative ideas can come to life. In roughly 90 minutes, we moved from the initial concept to a functional script, and Gemini even contributed to writing this very post!

Mail merge with Google App Scripts, send emails, Google Gmail , create a new mail merge with apps script

The goal: Personalized (and slightly mischievous) communication, powered by AI

The goal was to automate the process of sending personalized emails. But this wasn’t your typical mail merge. Instead of relying on pre-written templates, the idea was to leverage an AI to generate unique email content for each recipient, based on data from a Google Sheet. The twist? The emails were intended to be a lighthearted prank, sending dramatic monologues about everyday events to unsuspecting recipients.

The process: From brainstorm to script

Draft gmail message and email template with mail merge in Google Apps Script, send personalized emails

Here’s a breakdown of how we approached the task:

  1. The spark of an idea: A digital prank
  • The project began with a desire to play a harmless prank. I brainstormed several ideas, ranging from silly to slightly absurd.
  • The concept of sending dramatic readings of mundane events emerged as a favorite, and I realized that AI could be the perfect tool to bring this idea to life. Gemini, in particular, was great at providing me with the perfect examples of mundane, everyday events that could be used for the dramatic readings.
  1. Laying the foundation: Google Apps Script and Google Sheets
  • I started with Google Apps Script, a powerful tool that allows you to automate tasks within the Google Workspace ecosystem.
  • The data source was a Google Sheet, containing recipient information and variables to be used in the AI-generated content.
  1. Integrating AI: The Gemini API
  • To generate the personalized email content, I integrated the script with the Gemini API. Gemini allowed me to send prompts to the AI and receive generated text as a response.
  • The script constructs a dynamic prompt for each recipient, incorporating data from their respective row in the Google Sheet.
  1. Automation: Sending emails and scheduling
Send emails with mail merge, Google Sheet, gmail account, draft message, Google Docs, email template
  • The script uses the MailApp service to send the generated emails.
  • To make the process even more efficient, we set up a time-driven trigger to send emails on an hourly basis.

    5. Key challenges and solutions
  • Hourly execution and state management: I used PropertiesService to store the row number of the last sent email, ensuring that the script picks up where it left off after each hourly execution.
  • Concurrency: To prevent multiple instances of the script from interfering with each other, I implemented a locking mechanism using LockService.
  • Error handling: The script includes error handling for both Gemini API calls and the email sending process, with a fallback mechanism in place in case the AI fails to generate content.

The result: A powerful (and playful) tool

Create mail merge, Google Sheet, draft message with apps script, email template, mail template

The final script is a powerful tool that can be leveraged to send highly personalized emails at scale. In our case, it became a vehicle for delivering a series of amusing and unexpected emails. By combining the automation capabilities of Google Apps Script with the content generation power of AI, we created a solution that is both efficient and capable of generating highly personalized (and playful) content.

An example of the output

To give you a better sense of what the script produces, here’s an example of an email that was sent:

Subject: Your daily commute: A thrilling saga? 😉

Body:

(Dramatic monologue, generated by Gemini)

“But I know it for what it is: the gateway to purgatory. A slow, agonizing descent into… THE COMMUTE.

(They shudder, taking a large gulp of coffee.)

This caffeine, this… *nectar of the damned,* is all that stands between me and utter oblivion. Without it, I am a ghost, a husk, a creature doomed to wander the concrete canyons of despair!…”

A collaborative post-script

Create mail merge with a method using apps script

It’s worth noting that Gemini also played a significant role in helping me write this blog post. From refining my initial draft to suggesting ways to structure the information, Gemini’s assistance was invaluable. It’s pretty amazing to see AI helping with both the creative and the documentation sides of a project.

I hope this inspires you to explore the possibilities of AI in your own projects! Navigating the world of AI-powered tools can be exciting, but also complex. SADA’s experienced team provides the guidance and expertise you need to effectively leverage these technologies for increased productivity and seamless collaboration. You can start with our Gemini for Workspace Discovery Program. We’ll work with you to identify how Gemini can address your specific business challenges, provide hands-on training, and develop a clear roadmap for adoption, all within the secure and reliable Google infrastructure.

Contact us to learn more.

  • Kelly Wright

    Kelly Wright is a seasoned technology leader with over 15 years of experience in the IT industry. She leads a team of experts responsible for the successful deployment and management of Google Workspace collaboration tools. Kelly holds a Bachelor of Arts degree in Mathematics from Belmont University. She is also a certified Google Cloud Digital Leader, Google Associate Cloud Engineer, and Google Workspace Administrator.

LET'S TALK

Our expert teams of consultants, architects, and solutions engineers are ready to help with your bold ambitions, provide you with more information on our services, and answer your technical questions. Contact us today to get started.

Scroll to Top