Đang gửi ví dụ

Gửi bằng Python

Gửi thư điện tử từ Python với thư viện yêu cầu.

Điều kiện trước

  • Một Sendtern khóa API được lưu như SENDTERN_API_KEY
  • Python 3.8 or newer
  1. 1

    Yêu cầu cài đặt

    Bất kỳ trình khách HTTP nào cũng hoạt động. Hướng dẫn này sử dụng requests.

    1pip install requests
    
  2. 2

    Gửi thư điện tử

    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

    Kiểm tra phản ứng

    Một yêu cầu thành công sẽ trả lại 200 OK với id email. Dùng nó để tìm email sau này.

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

Thay thế người gửi ví dụ bằng một địa chỉ trên domain xác thực của bạn.