Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Beginner ⏱ 20 min

📧 Build an AI Email Subject Generator with Gemini

🎯 What You'll Build

A Python tool that takes any email body and generates five subject line variations in different tones — professional, friendly, urgent, and curiosity-driven — using Gemini.

📋 What You'll Need

1

Set up Gemini API

Get a free key from Google AI Studio and configure the SDK.

pip install google-generativeai
import google.generativeai as genai
import os

genai.configure(api_key=os.environ.get('GEMINI_API_KEY') or 'your-api-key-here')
model = genai.GenerativeModel('gemini-1.5-flash')

# Quick test
r = model.generate_content("Say hello in 5 words.")
print(r.text)
2

Generate 5 subject line variations

Prompt Gemini to write subject lines in five distinct tones.

def generate_subjects(email_body):
    prompt = f"""You are an email marketing expert.
Read the email body below and generate exactly 5 subject lines — one for each of these tones:
1. Professional / Formal
2. Friendly / Conversational
3. Urgent / Time-sensitive
4. Curiosity-driven / Intriguing
5. Direct / Clear benefit

Format your response EXACTLY like this:
1. [Professional]: subject here
2. [Friendly]: subject here
3. [Urgent]: subject here
4. [Curiosity]: subject here
5. [Direct]: subject here

Email body:
{email_body}"""

    response = model.generate_content(prompt)
    return response.text

email = """
Hi team,

I wanted to let you know that we have redesigned our onboarding process.
New users can now complete setup in under 5 minutes instead of 30.
The new flow includes video guides, tooltips, and a progress checklist.
Please review the attached document and share your feedback by Friday.

Thanks,
Sarah
"""

print(generate_subjects(email))
1. [Professional]: New Onboarding Process: Review Required by Friday
2. [Friendly]: We've made onboarding way easier — check it out!
3. [Urgent]: Action Required: Onboarding Review Due This Friday
4. [Curiosity]: What if new users could be fully set up in 5 minutes?
5. [Direct]: New 5-Minute Onboarding Flow — Your Feedback Needed
3

Interactive CLI tool

Build a loop so users can paste emails and instantly get subjects.

def main():
    print("AI Email Subject Generator (powered by Gemini)")
    print("Paste your email body below. Type DONE on a new line when finished.\n")

    while True:
        lines = []
        while True:
            line = input()
            if line.strip() == 'DONE': break
            lines.append(line)

        body = '\n'.join(lines).strip()
        if not body:
            print("No email content — exiting."); break

        print("\nGenerating subject lines...\n")
        result = generate_subjects(body)
        print(result)
        print("\n" + "="*50)
        print("Generate for another email? (paste it, or press Ctrl+C to exit)\n")

main()
💡 Tip: Add ?output_format="json" instructions to the prompt to get structured JSON output instead of formatted text. This makes it easy to display subjects in a web UI or store them in a database.

🎉 You Did It!

A 40-line tool that could save hours of email copywriting every week. The same pattern — describe the task in natural language, specify the output format, parse the response — works for product descriptions, SEO meta tags, ad copy, and any other text generation task.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.