送出例子

用 Python 發送

使用 requests 函式庫從 Python 發送電子郵件。

前提條件

  • 存儲為 SENDTERN_API_KEY 的 Sendtern API 鍵
  • Python 3.8 or newer
  1. 1

    安裝請求

    任何HTTP客戶端都可以使用,本指南使用requests。

    1pip install requests
    
  2. 2

    發送電子郵件

    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

    檢查一下反應

    成功的請求返回 200 OK 與電子郵件 ID。使用它以後查看電子郵件。

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

用您的 verified domain 的地址替换示例发件人。