Elena' s AI Blog

Sending Emails with Python and receiving your messages

29 May 2024 (updated: 17 Aug 2026) / 23 minutes to read

Elena Daehnhardt


Midjourney 6.0, May 2024


If you click an affiliate link and subsequently make a purchase, I will earn a small commission at no additional cost (you pay nothing extra). This is important for promoting tools I like and supporting my blogging.

I thoroughly check the affiliated products' functionality and use them myself to ensure high-quality content for my readers. Thank you very much for motivating me to write.



TL;DR:
  • For a static site, handle incoming messages with form endpoints like UseBasin. For sending emails, use Python's smtplib with Gmail. For modern security, set up OAuth2.0 via Google Cloud Console instead of using less secure app passwords.

📚 This post is part of the "Managing Emails" series

Series: Managing Emails (Part 8 of 8)

Previous: Part 1 — My Inbox Is a Black Hole (and How I Escaped It)

Sending Emails with Python on a Static Site: Overview

smtplib is the standard-library Python module that sends email over SMTP (Simple Mail Transfer Protocol), and the Gmail API is Google’s REST interface for sending mail with OAuth2 tokens instead of a stored password. This post uses both to send newsletters from a backend-less GitHub Pages site.

If you’re hoping to send email via API with no authentication at all, I’ll be upfront: no reliable method skips it entirely. Gmail’s SMTP server needs a password or app password, the Gmail API needs an OAuth2 token, and even the free tiers of transactional email APIs like SendGrid or Mailgun require an API key. That’s a deliberate anti-spam safeguard rather than an oversight, so this post focuses on the two authenticated Python methods that actually work.

Because this blog is a static website hosted on GitHub Pages (which I wrote about in my AI-Free Website Design post), I do not have a backend server. That means I cannot simply run a PHP script or a Node.js process to handle contact forms or send out newsletters.

But that hasn’t stopped me. I use a hybrid approach: a lightweight service for receiving messages, and pure Python code for sending them.

Receiving Form Submissions on a Static Site with a Form Endpoint

To handle incoming form submissions on a static site, you need an endpoint. For years, I have used UseBasin.com.

The setup is incredibly straightforward: you generate an HTML form on their dashboard, copy the action URL, and paste it into your site’s HTML. You can customise the form styling entirely with your own CSS.

I prefer it over other solutions because it handles spam filtering effectively and allows me to export my subscriber list easily. Once I download the new subscribers as a CSV file to my local machine, I am ready for the fun part: sending the emails with Python!

Sending Email Messages with Python smtplib and the Gmail API

🔒 Subscribe to keep reading.

References

🔒 Subscribe to keep reading.

Subscribe to unlock the full article ❤️

The form below signs you up for the newsletter. It does not log you into the app — log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2024) 'Sending Emails with Python and receiving your messages', daehnhardt.com, 29 May 2024. Available at: https://daehnhardt.com/blog/2024/05/29/sending-emails-with-python/
All Posts