Send email with Flask
A server-side call to the SendSend API from your Flask app.
- Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
- Set SENDSEND_API_KEY in the environment that runs Flask.
- Add this to
app.py:
import os, uuid, requests
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.post("/send")
def send():
r = requests.post(
"https://api.sendsend.email/v1/emails",
headers={"Authorization": f"Bearer {os.environ['SENDSEND_API_KEY']}", "Idempotency-Key": str(uuid.uuid4())},
json={"from": "Acme <hello@mail.acme.com>", "to": [request.json["to"]], "subject": "Hello from Flask", "html": "<p>It works!</p>"},
timeout=15,
)
return jsonify(r.json()), r.status_codeGood to know
- A
202response means the message was queued, not delivered. See message states and webhooks. - Always send an Idempotency-Key, so a retry cannot send twice.
- Check the response status. Errors have one shape: see Errors.
- More options: attachments, scheduling, headers, templates and React Email.