AI Email Generator for Gmail Apps Script 19

AI Email Generator for Gmail 🚀 Apps Script + Gemini Mastery — Issue #19

https://github.com/lsvekis/Apps-Script-Code-Snippets

AI Email Generator for Gmail

Automatically generate complete, context-aware emails inside Gmail using Google Apps Script + Gemini.


⭐ What You Will Build

In this issue, you’ll build an AI Email Generator that can:

📧 Draft full emails from a simple prompt
🧠 Adjust tone (professional, friendly, persuasive)
⚡ Generate replies to existing emails
✍️ Create follow-ups, outreach, and summaries
📤 Insert drafts directly into Gmail


🧠 Why This Project Matters

Writing emails is one of the most common tasks — and one of the most repetitive.

Instead of thinking:

“How should I write this email?”

You can simply say:

“Write a follow-up email after a meeting with a client”

And get a complete, ready-to-send draft.


🧩 Architecture

User prompt

Apps Script (Gmail context)

Gemini generates email

Apps Script creates draft in Gmail


🧱 Step 1 — Create the Menu

Code.gs

function onOpen() {GmailApp.getUi()
.createMenu("AI Tools")
.addItem("AI Email Generator", "showEmailSidebar")
.addToUi()}function showEmailSidebar(){GmailApp.getUi().showSidebar(
HtmlService.createHtmlOutputFromFile("Sidebar")
.setTitle("AI Email Generator")
)}

🧱 Step 2 — Sidebar UI

Sidebar.html

<div style="font-family:Arial;padding:14px;"><h2>AI Email Generator</h2><label><b>Describe the email</b></label><textarea id="prompt"
style="width:100%;height:80px;">
Example: Write a follow-up email after a meeting with a client
</textarea><button onclick="generate()">Generate Email</button><pre id="output"
style="white-space:pre-wrap;margin-top:12px;"></pre><script>function generate(){document.getElementById("output").textContent="Generating email..."google.script.run
.withSuccessHandler(res=>{
document.getElementById("output").textContent=res
})
.generateEmail(
document.getElementById("prompt").value
)}</script></div>

🧱 Step 3 — Generate Email with Gemini

EmailGenerator.gs

function generateEmail(userPrompt){if(!userPrompt) return "Enter an email request."const prompt = `
You are an expert email writer.Instruction:
${userPrompt}Generate a complete email including:
- Subject line
- Greeting
- Body
- ClosingKeep tone appropriate and clear.
`let resulttry{result = callGemini(prompt,"")}catch(e){return "Gemini error: "+e}return createDraft_(result)}

🧱 Step 4 — Create Gmail Draft

DraftCreator.gs

function createDraft_(emailContent){const subjectMatch = emailContent.match(/Subject:(.*)/i)const subject = subjectMatch ? subjectMatch[1].trim() : "AI Generated Email"const body = emailContent.replace(/Subject:.*\n/i,"")GmailApp.createDraft("", subject, body)return "Draft email created in Gmail."
}

🧱 Step 5 — Gemini Helper

Reuse from previous issues:

const GEMINI_API_KEY = "YOUR_API_KEY"
const GEMINI_MODEL = "gemini-2.5-flash"

🧪 Example Prompts

Users can ask:

Write a follow-up email after a client meeting
Create a cold outreach email for a web development service
Write a polite reminder email for overdue payment
Respond to a customer complaint professionally

🔥 Advanced Exercises

Readers can extend this by adding:

✅ reply to existing email threads
✅ tone selector dropdown
✅ signature auto-insert
✅ personalized placeholders
✅ email templates library