Introduction
smtplib works — but in 2026, sending email from Python usually means wrestling with SMTP ports, app passwords, spam filters, and deliverability. Resend is the modern alternative: one HTTP call, great deliverability, and a generous free tier.
This post shows how to send emails from Python with Resend — plain text, HTML, attachments, and a real Streamlit contact form. (It’s the same API powering this site’s contact form.)
Setup
pip install resend python-dotenv
Create a Resend account (free: 3,000 emails/month), verify your domain, and grab an API key. Store it:
# .env
RESEND_API_KEY=re_xxxxxxxxxxxx
Step 1: Send a plain-text email
import os, resend
from dotenv import load_dotenv
load_dotenv()
resend.api_key = os.environ["RESEND_API_KEY"]
params = {
"from": "Tech With Galvan <[email protected]>",
"to": ["[email protected]"],
"subject": "Hello from Python",
"text": "This email was sent with the Resend API. No SMTP required.",
}
email = resend.Emails.send(params)
print(email["id"]) # e.g. "a1b2c3..."
That’s it. No SMTP host, no port 587, no TLS negotiation. One POST request.
Step 2: HTML email with a template
html = """
<h1>Welcome, {name}!</h1>
<p>Thanks for signing up. Your account is ready.</p>
<a href="https://techwithgalvan.in">Visit the site</a>
""".format(name="Galvan")
resend.Emails.send({
"from": "Tech With Galvan <[email protected]>",
"to": ["[email protected]"],
"subject": "Welcome!",
"html": html,
})
Resend also supports React Email templates if you want component-based emails — but plain HTML strings work fine for most cases.
Step 3: Attachments
import base64
with open("report.pdf", "rb") as f:
data = base64.b64encode(f.read()).decode()
resend.Emails.send({
"from": "[email protected]",
"to": ["[email protected]"],
"subject": "Monthly report",
"text": "Attached.",
"attachments": [{"filename": "report.pdf", "content": data}],
})
Step 4: Reply-To and CC/BCC
resend.Emails.send({
"from": "[email protected]",
"to": ["[email protected]"],
"reply_to": "[email protected]", # replies go to the user
"cc": ["[email protected]"],
"bcc": ["[email protected]"],
"subject": "New inquiry",
"text": "Message body here.",
})
reply_to is the key feature for contact forms — the recipient hits “Reply” and it goes to the original sender, not your noreply address.
Step 5: Streamlit contact form (full example)
import streamlit as st, os, resend
from dotenv import load_dotenv
load_dotenv()
resend.api_key = os.environ["RESEND_API_KEY"]
with st.form("contact"):
name = st.text_input("Name")
email = st.text_input("Email")
msg = st.text_area("Message")
if st.form_submit_button("Send"):
if not name or not email or not msg:
st.error("Fill all fields")
else:
resend.Emails.send({
"from": "[email protected]",
"to": [os.environ["CONTACT_TO"]],
"reply_to": email,
"subject": f"New message from {name}",
"text": f"Name: {name}\nEmail: {email}\n\n{msg}",
})
st.success("Sent! I'll reply within 24-48h.")
This is essentially what this site’s contact page does — a Pages Function receives the POST, validates, and calls Resend.
Resend vs SMTP vs SendGrid
| SMTP (smtplib) | SendGrid | Resend | |
|---|---|---|---|
| Setup | App passwords, ports | API key | API key |
| Deliverability | Often lands in spam | Good | Excellent (modern infra) |
| Free tier | Gmail: 500/day | 100/day | 3,000/month |
| DX | Verbose | OK | Excellent |
| Templates | Manual | Built-in | React Email |
Common mistakes
- Sending from a generic domain — verify your own domain in Resend or emails land in spam.
[email protected]won’t work. - Hardcoding the API key — always use env vars; never commit keys.
- No retry on failure — Resend can rate-limit; wrap calls in try/except and show a friendly error.
- Forgetting
reply_to— otherwise replies go to your noreply address and you miss them. - Trusting user input — validate email format and sanitize subject/body before sending (prevents header injection).
Key Concepts
- Resend API = one HTTP call to send email
from= verified sender addressreply_to= where replies land- Attachments = base64-encoded content
- Domain verification = the #1 deliverability factor
What to Try Next
- Add email notifications to your habit tracker — daily reminder emails.
- Build a newsletter digest that emails you a weekly summary of your blog posts.
- Combine with Streamlit for a “contact me” widget in any app.
- Set up a Cloudflare Pages Function (like this site) to keep the API key server-side.
FAQ
Is Resend free enough for a personal site?
Yes — 3,000 emails/month free, and 100/day on the free tier. Most personal sites send <100 emails/month.
Resend vs AWS SES?
SES is cheaper at scale (pennies per 1000) but requires more setup (sandbox mode, domain verification, bounce handling). Resend is the better DX for small/medium projects; SES for high-volume.
Can I send from a Gmail address?
No — you must verify a domain you own. For testing, Resend gives you an [email protected] sender. For production, verify your domain.