Back to blog
TutorialFebruary 5, 20267 min read

How to Send Emails in Python (2026 Guide)

Learn how to send emails from Python using REST APIs and smtplib. With code examples for Django, Flask, and FastAPI.

Sending Emails from Python

Python has built-in email support via smtplib, but using a REST API is simpler and more reliable for production applications.

Method 1: REST API with requests

import requests

response = requests.post(
    "https://api.unosend.co/v1/emails",
    headers={
        "Authorization": "Bearer un_your_api_key",
        "Content-Type": "application/json",
    },
    json={
        "from": "hello@yourdomain.com",
        "to": "user@example.com",
        "subject": "Hello from Python!",
        "html": "<h1>Welcome!</h1><p>Sent from Python.</p>",
    },
)

print(response.json())

Method 2: Using the Unosend Python SDK

pip install unosend
from unosend import Unosend

client = Unosend(api_key="un_your_api_key")

result = client.emails.send({
    "from": "hello@yourdomain.com",
    "to": "user@example.com",
    "subject": "Welcome!",
    "html": "<h1>Hello!</h1>",
})

print(f"Email sent: {result['id']}")

Django Integration

# settings.py
UNOSEND_API_KEY = os.environ.get("UNOSEND_API_KEY")

# views.py
from unosend import Unosend
from django.conf import settings

unosend = Unosend(api_key=settings.UNOSEND_API_KEY)

def send_welcome_email(user):
    unosend.emails.send({
        "from": "welcome@yourapp.com",
        "to": user.email,
        "subject": f"Welcome, {user.first_name}!",
        "html": f"<h1>Welcome to our app, {user.first_name}!</h1>",
    })

Flask Example

from flask import Flask, request, jsonify
from unosend import Unosend

app = Flask(__name__)
unosend = Unosend(api_key=os.environ["UNOSEND_API_KEY"])

@app.route("/api/contact", methods=["POST"])
def contact():
    data = request.json
    result = unosend.emails.send({
        "from": "contact@yourapp.com",
        "to": "team@yourapp.com",
        "subject": f"Contact: {data['name']}",
        "html": f"<p>{data['message']}</p>",
    })
    return jsonify({"success": True, "id": result["id"]})

FastAPI Example

from fastapi import FastAPI
from pydantic import BaseModel
from unosend import Unosend

app = FastAPI()
unosend = Unosend(api_key=os.environ["UNOSEND_API_KEY"])

class ContactForm(BaseModel):
    name: str
    email: str
    message: str

@app.post("/api/contact")
async def contact(form: ContactForm):
    result = unosend.emails.send({
        "from": "contact@yourapp.com",
        "to": "team@yourapp.com",
        "subject": f"Contact: {form.name}",
        "html": f"<p>From: {form.name}</p><p>{form.message}</p>",
    })
    return {"success": True}

Production Tips

  • Use environment variables — Never hardcode API keys
  • Async sending — Use httpx or aiohttp for async Python apps
  • Retry on failure — Use tenacity library for retry logic
  • Validate emails first — Check email format before sending
  • Track delivery — Set up webhooks for bounce and complaint handling
PythonDjangoFlaskTutorial

Ready to send your first email?

Get started with 5,000 free emails/month. No credit card required.

Start for free