Sending examples

Send with Python

Send email from Python with the requests library.

Prerequisites

  • A Sendtern API key stored as SENDTERN_API_KEY
  • Python 3.8 or newer
  1. 1

    Install requests

    Any HTTP client works. This guide uses requests.

    1pip install requests
    
  2. 2

    Send an email

    Create send.py.

    1# send.py
    2import os
    3import uuid
    4import requests
    5
    6res = requests.post(
    7    "https://sendtern.com/emails",
    8    headers={
    9        "Authorization": f"Bearer {os.environ['SENDTERN_API_KEY']}",
    10        "Idempotency-Key": str(uuid.uuid4()),
    11    },
    12    json={
    13        "from": "Acme <hello@mail.example.com>",
    14        "to": ["you@example.com"],
    15        "subject": "Hello from Python",
    16        "html": "<p>It works!</p>",
    17    },
    18    timeout=10,
    19)
    20
    21res.raise_for_status()
    22print(res.json())
    
  3. 3

    Check the response

    A successful request returns 200 OK with the email id. Use it to look the email up later.

    1{
    2  "id": "47be30ca-24b7-48fb-b35d-ff287b89e66d"
    3}
    

Replace the example sender with an address on your verified domain.