EnglishEN

Get a daily morning news summary email with the Gemini API: your first Python automation

Build one Python file that collects news headlines, has Gemini summarize them in five lines, and sends the summary to your Gmail. Ask an AI to write the code; this guide only covers how to put it in place and run it.

Where to gopython.orgTerminalmyaccount.google.com/apppasswords

Steps

  1. Install Python from https://www.python.org/downloads/. On a Mac, after installing, double-click Install Certificates.command in Applications › the Python 3.xx folder to run it once. On Windows, if the installer shows a box or question about adding it to PATH, choose to add it (source: https://docs.python.org/3/using/windows.html).
  2. Check the version in Terminal (Command Prompt on Windows). It must show Python 3.10 or later. This code does not run on the 3.9 that comes with a Mac.
    python3 --version
  3. Put this into the Claude, ChatGPT or Gemini chat window to get the code.
    Please make one Python file.
    1. Read 10 article titles and links from this RSS address: https://news.google.com/rss/search?q=AI&hl=en-US&gl=US&ceid=US:en
    2. Make a five-line summary in English with the Gemini API (the google-genai library). Tell it to attach the original link to each line and to add nothing that is not in the list.
    3. Send the summary with Gmail (smtp.gmail.com, app password).
    Do not write the API key, mail address or app password in the code; read them from the environment variables GEMINI_API_KEY, MAIL_FROM, MAIL_TO, MAIL_APP_PASSWORD.
    Use only one external library, google-genai.
  4. Make a work folder and a venv (work box), and install the library. This is for Mac; on Windows, use python -m venv .venv and .venv\Scripts\activate instead. When (.venv) shows at the start of the line, you are inside the box.
    mkdir news && cd news
    python3 -m venv .venv
    source .venv/bin/activate
    pip install -U google-genai
  5. Create news_mail.py in the news folder and paste it in. If the code the AI gave you is very different, you can use this code as is.
    import os
    import smtplib
    import urllib.request
    import xml.etree.ElementTree as ET
    from email.message import EmailMessage
    
    from google import genai
    
    # 1) News feeds to read (RSS) — change them to any you like
    FEEDS = [
        "https://news.google.com/rss/search?q=AI&hl=en-US&gl=US&ceid=US:en",
    ]
    HOW_MANY = 10  # articles to take from each feed
    
    # 2) Mail settings — values come from environment variables, not the code
    MAIL_FROM = os.environ["MAIL_FROM"]          # Gmail address to send from
    MAIL_TO = os.environ.get("MAIL_TO", MAIL_FROM)  # address to send to (yourself if unset)
    MAIL_APP_PASSWORD = os.environ["MAIL_APP_PASSWORD"]  # Gmail app password
    
    def read_titles(url):
        req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
        with urllib.request.urlopen(req, timeout=20) as r:
            root = ET.fromstring(r.read())
        items = root.findall("./channel/item")[:HOW_MANY]
        return [f"- {i.findtext('title')} ({i.findtext('link')})" for i in items]
    
    def summarize(lines):
        client = genai.Client()  # reads the GEMINI_API_KEY environment variable
        prompt = (
            "Below is a list of today's news headlines. Summarize them in English in five lines or fewer. "
            "Write each line as «one sentence on what happened + the original link», and add nothing that is not in the list.\n\n"
            + "\n".join(lines)
        )
        interaction = client.interactions.create(model="gemini-4-hydrogen", input=prompt)
        return interaction.output_text
    
    def send_mail(body):
        msg = EmailMessage()
        msg["Subject"] = "This morning's news summary"
        msg["From"] = MAIL_FROM
        msg["To"] = MAIL_TO
        msg.set_content(body)
        with smtplib.SMTP_SSL("smtp.gmail.com", 465) as s:
            s.login(MAIL_FROM, MAIL_APP_PASSWORD)
            s.send_message(msg)
    
    if __name__ == "__main__":
        lines = []
        for url in FEEDS:
            lines += read_titles(url)
        print(f"Read {len(lines)} articles.")
        summary = summarize(lines)
        print(summary)
        send_mail(summary)
        print("Mail sent.")
    
    Terminal screen after running the news summary code
    Terminal screen after running the news summary code
    This morning's news summary email arrived in Gmail
    This morning's news summary email arrived in Gmail
  6. Create a Gmail app password. 2-Step Verification must be on for your Google account; at https://myaccount.google.com/apppasswords, enter an app name (such as News summary) and create it to get 16 characters. The screen shows them with spaces, but you can enter them without the spaces.
  7. Put your mail address and app password in environment variables (Mac). On Windows, set them like setx MAIL_FROM "you@gmail.com", then open a new window and activate again.
    echo 'export MAIL_FROM="you@gmail.com"' >> ~/.zshrc
    echo 'export MAIL_APP_PASSWORD="YOUR_16_CHARACTERS"' >> ~/.zshrc
    source ~/.zshrc
  8. Run it. You are done when it prints «Read 10 articles.» → five summary lines → «Mail sent.» and «This morning's news summary» lands in your inbox.
    python news_mail.py

Change the news feed

To get other news, change q=AI in FEEDS to the search term you want, or add one more line with the RSS address of a news site you read. For all tech news, use https://news.google.com/rss/headlines/section/topic/TECHNOLOGY?hl=en-US&gl=US&ceid=US:en.

The model gemini-4-hydrogen has a free tier on the pricing page. The pricing page notes that some older models move to other models from 2026-12-01, so if the AI put in a model name not in this guide, change it to this name.

«Every morning by itself» has to run even with your computer off, so we add it later, in the server parts of this series.

Troubleshooting

  • CERTIFICATE_VERIFY_FAILED (Mac): you did not run Install Certificates.command. Run it, then try again.
  • KeyError: 'MAIL_FROM': the mail environment variables were not set, or a name is wrong.
  • AttributeError: 'Client' object has no attribute 'interactions': you made the venv with the Mac's built-in Python 3.9. Install 3.10 or later, make the venv again, then run pip install -U google-genai again.
  • BadRequestError 400 API key not valid: the key was pasted wrong. Fix it as in «Troubleshooting» in Part 2.
  • ValueError: No API key was provided.: the Gemini key could not be found. Use the check command from Part 2 to see whether GEMINI_API_KEY shows up.
  • Mail login fails: usually you entered your normal password, or 2-Step Verification is off. Enter the 16-character app password.
  • ModuleNotFoundError: No module named 'google': you ran it outside the venv box. Activate it so (.venv) shows at the start of the line, then run it.
  • Accounts that use only security keys, work or school accounts, and Advanced Protection accounts may not see the app passwords menu.

Source

Related posts