Skip to main content

Official Python SDK for the YouSend email API

Project description

yousend (Python)

Official Python SDK for the YouSend email API. Zero dependencies (stdlib only), Python 3.8+.

Install

pip install yousend

Quick start

from yousend import YouSend

ys = YouSend("ys_live_...")

resp = ys.emails.send(
    from_="hello@yourdomain.com",
    to="customer@example.com",
    subject="Welcome aboard",
    html="<h1>Welcome!</h1><p>Thanks for signing up.</p>",
    track_opens=True,
    track_clicks=True,
)
print(resp["messages"][0]["id"])

from is a Python keyword, so the send parameter is from_.

Sending

# Multiple recipients
ys.emails.send(from_=sender, to=["a@x.com", "b@x.com"], subject=s, html=h)

# Template + variables
ys.emails.send(from_=sender, to=to, template="welcome", data={"name": "Sam"})

# Idempotent send (safe to retry)
ys.emails.send(from_=sender, to=to, subject=s, html=h, idempotency_key="order-1234")

# Batch — up to 100, one recipient each
ys.emails.batch([
    {"from_": sender, "to": "a@x.com", "subject": s, "html": h},
    {"from_": sender, "to": "b@x.com", "subject": s, "html": h},
])

# Retrieve / list
email = ys.emails.get(message_id)
recent = ys.emails.list(status="delivered", limit=50)

Other resources

ys.domains.create("yourdomain.com")   # returns DNS records to publish
ys.domains.verify(domain_id)
ys.templates.upsert(name="welcome", subject="Hi {{name}}", html="<p>Hi {{name}}</p>")
ys.webhooks.create(url="https://you.com/hooks", events=["email.delivered", "email.opened"])
ys.suppressions.add("bounced@x.com")
ys.api_keys.create("live")            # returns the key once

Verifying webhooks

Pass the raw request body — not a re-serialized dict.

Flask

from flask import Flask, request
from yousend import verify_webhook, YouSendError

app = Flask(__name__)

@app.post("/webhooks/yousend")
def hook():
    try:
        event = verify_webhook(request.get_data(), request.headers.get("Courier-Signature"), SECRET)
    except YouSendError:
        return "", 400
    if event["type"] == "email.clicked":
        print("clicked", event["data"]["url"])
    return "", 200

FastAPI

from fastapi import FastAPI, Request, Response
from yousend import verify_webhook, YouSendError

app = FastAPI()

@app.post("/webhooks/yousend")
async def hook(request: Request):
    raw = await request.body()
    try:
        event = verify_webhook(raw, request.headers.get("Courier-Signature"), SECRET)
    except YouSendError:
        return Response(status_code=400)
    return Response(status_code=200)

Errors

from yousend import YouSend, YouSendError

try:
    ys.emails.send(from_=sender, to=to, subject=s, html=h)
except YouSendError as e:
    print(e.status, e.code, e.message)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yousend-0.1.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yousend-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file yousend-0.1.0.tar.gz.

File metadata

  • Download URL: yousend-0.1.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for yousend-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bdbc34505a492c527afe8284204c8661db590228c61792de9a8d91b1f0dff2ad
MD5 9d12e79456e5129d496ce17bf2fd61bc
BLAKE2b-256 f7d9b621b52e8d1849a0dc08aa956b0e4a756f093852a13618b9a72c063b99ab

See more details on using hashes here.

File details

Details for the file yousend-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: yousend-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for yousend-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b4e0cf9a0970e7caf69623c91f080f8677e4b31a83aaa8da63abce3cd45c240
MD5 5a44904af5cc6373ef4fe352a87ff5c9
BLAKE2b-256 803e0b3e0e0246e9786f5563877486d916f295ef45b5fa5f3061f1212c0f00f9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page