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
We will cover the necessary steps, including setting up your Gmail account, using Python’s smtplib module, and configuring all the essential security settings in Gmail.
References
🔒 Subscribe to keep reading.
You've hit a Deep Dive tutorial.
I spend dozens of hours researching, coding, and breaking things to write these guides. This content is free, but reserved for my subscriber community. Drop your email below to unlock this guide (and all past/future deep dives):
Full content temporarily unavailable — refresh in a moment
Already a subscriber? Use the magic link from your last newsletter, or reset your password.
Log in to unlock
New subscribers get an inbox mail: Set a password to unlock articles. The form does not log you in — use the same email afterwards.