SMTP 與 HTTP API
建立 API 金鑰並透過 SMTP 或 HTTP API 傳送郵件,包含請求欄位、回應、錯誤碼與範例。
你可以用兩種方式透過發信流寄信:從任何應用程式或郵件函式庫走 SMTP,或使用 HTTP API。兩者使用相同的 API 金鑰、配額、抑制名單與事件。開始之前,請先驗證發信網域並建立 API 金鑰。
API 金鑰
- 開啟發信流,進入 API 金鑰 分頁。
- 點擊 建立金鑰,輸入 名稱(例如
production)並確認。 - 複製顯示的金鑰,然後點擊 我已儲存。金鑰只會顯示這一次,之後清單只顯示它的 前綴,例如
ytm_aB3dE6gH…。

每把金鑰都以 ytm_ 開頭。一個發信流最多可有 20 把有效金鑰。清單會顯示每把金鑰上次使用的時間。要停用金鑰,請點擊 撤銷:使用它的程式會立即失效。金鑰只對所屬的發信流有效。
請把 API 金鑰保存在伺服器端。不要放在瀏覽器程式碼、行動 App 或公開的程式碼庫中。任何取得金鑰的人都能以你的網域名義寄信。
SMTP
發信流的 概覽 分頁會顯示 SMTP 設定:
| 設定 | 值 |
|---|---|
| 主機 | 發信流顯示的 SMTP 主機,通常是 smtp.tm.yeapcloud.net |
| 連接埠 | 587(STARTTLS)或 465(TLS) |
| 使用者名稱 | apikey(就是這個字) |
| 密碼 | 此發信流的一把 API 金鑰 |
| 類別標頭 | X-Yeap-Category |
請使用控制台顯示的主機。有些發信流會透過不同的伺服器群組寄送,主機名稱也不同。
SMTP 發信的運作方式:
- From 標頭必須使用該發信流已驗證網域下的地址。
- 每位收件人(To、Cc 與 Bcc)都會收到獨立的副本與獨立的郵件 ID,各計為一封郵件。
- 要設定類別,請加入
X-Yeap-Category: password-reset這類標頭。投遞前會移除該標頭。可用字元為字母、數字、.、_與-,最多 64 個字元。 - 每封郵件最大 25 MB,每個 SMTP 交易最多 1,000 位收件人。
- 每封郵件都會加上
X-TM-ID標頭(即傳送記錄與事件中的郵件 ID),以及供信箱服務商回饋迴路使用的Feedback-ID標頭。 - 在行銷發信流上,你設定的任何
List-Unsubscribe標頭都會被替換為 Yeap Cloud 的一鍵退訂標頭。SMTP 不會修改郵件內文,因此請自行在內容中加入實體地址與退訂連結。請參閱抑制名單與退訂。 - 開啟追蹤與點擊追蹤只能透過 HTTP API 使用。
使用 Python 標準函式庫的範例:
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["From"] = "Example <noreply@example.com>"
msg["To"] = "user@example.org"
msg["Subject"] = "Reset your password"
msg["X-Yeap-Category"] = "password-reset"
msg.set_content("Click the link to reset your password: https://example.com/reset/abc")
with smtplib.SMTP("smtp.tm.yeapcloud.net", 587) as s:
s.starttls()
s.login("apikey", "ytm_your_api_key")
s.send_message(msg)SMTP 回應碼
伺服器拒絕郵件時,回應碼會告訴你原因。4xx 為暫時性錯誤:請稍後重試。5xx 為永久性錯誤:請先修正原因再重新寄送。
| 代碼 | 訊息 | 原因與處理方式 |
|---|---|---|
535 5.7.8 | Authentication failed / API key revoked | 使用者名稱或金鑰錯誤,或金鑰已被撤銷。使用者名稱必須是 apikey。 |
454 4.7.0 | Temporary authentication failure / Too many authentication failures | 伺服器尚未就緒,或你的 IP 登入失敗次數過多。請稍候再試。 |
451 4.7.1 | Stream is paused | 發信流已被自動暫停。請查看發信流上的原因並聯絡客服。 |
550 5.7.1 | Stream is banned / suspended / terminated | 發信流已被封禁、訂閱已暫停,或已被釋放。 |
550 5.7.1 | From domain … is not verified for this stream | 請驗證網域,或確認它已共用或綁定到此發信流。 |
550 5.7.1 | Sandbox mode: recipient is not a verified address | 發信流處於沙箱中。請驗證收件人,或寄到你自己的網域。 |
550 5.7.1 | Recipient is on a suppression list / All recipients are suppressed | 該地址在抑制名單中(退信、投訴、退訂或手動加入)。 |
452 4.7.1 | Hourly sending limit reached, try later | 每小時限額(或沙箱每日限額)已用完。請在下一個小時重試。 |
550 5.7.1 | Monthly sending quota exhausted | 每月額度已用完且不允許超量:免費方案,或已達超量消費上限。 |
452 4.5.3 | Too many recipients | 單一交易的收件人超過 1,000 位。 |
553 5.1.3 | Invalid recipient address | 收件人地址格式錯誤。 |
554 5.7.1 | Message rejected: virus detected | 郵件含有病毒。發信流會立即被封禁,所有 API 金鑰都會被撤銷。 |
554 5.7.1 | Message rejected as spam | 內容被判定為垃圾郵件。多次被拒會導致發信流暫停。 |
451 4.3.0 | Content scanner unavailable, try again later | 行銷郵件必須通過掃描才會被接受。請稍後重試。其他 451 4.3.0 回應為暫時性伺服器錯誤:請重試。 |
554 5.6.0 | Malformed message headers | 請修正郵件標頭。 |
HTTP API
| 項目 | 值 |
|---|---|
| 基礎 URL | https://api.tm.yeapcloud.net/v1(也可使用 https://api.yeap.cloud/api/tm/v1) |
| 認證 | Authorization: Bearer ytm_your_api_key |
| 格式 | 請求與回應內文皆為 JSON,Content-Type: application/json |
| 速率限制 | 每把 API 金鑰每分鐘 600 個請求 |
錯誤會回傳 HTTP 狀態碼,以及類似以下的內文:
{"error": {"code": "invalid_request", "message": "subject is required (single line)"}}| HTTP 狀態 | error.code | 說明 |
|---|---|---|
| 400 | invalid_json, invalid_request | 內文不是 JSON 物件,或批次為空或過大 |
| 401 | unauthorized | API 金鑰缺少、格式錯誤、已撤銷或不存在 |
| 403 | stream_paused, stream_banned, stream_suspended | 發信流無法發信 |
| 403 | from_domain_unverified, invalid_from | From 網域尚未為此發信流驗證,或 From 地址無效 |
| 403 | postal_address_required | 行銷發信流未填寫實體地址。請在 行銷設定 分頁填寫。 |
| 403 | virus | 附件含有病毒。發信流已被封禁。 |
| 404 | not_found | 此發信流中不存在該郵件 |
| 422 | invalid_request | 欄位缺少或無效,message 會指出是哪一個。 |
| 429 | rate_limited | 一分鐘內超過 600 個請求。請放慢速度後重試。 |
| 503 | no_node | 目前沒有可用的發信伺服器。請稍後重試。 |
傳送郵件:POST /v1/send
| 欄位 | 類型 | 說明 |
|---|---|---|
from | string | 必填。單一地址,例如 noreply@example.com 或 Example <noreply@example.com>。網域必須已為此發信流驗證。 |
to | string 或 array | 必填。單一地址、以逗號分隔的字串或陣列。陣列項目也可以是物件 {"email": "...", "name": "..."}。 |
cc, bcc | string 或 array | 選填。格式與 to 相同。Bcc 地址不會出現在標頭中。 |
reply_to | string 或 array | 選填。Reply-To 地址。 |
subject | string | 必填。單行,最多 998 個字元。 |
html, text | string | 至少需要其中一個。兩者都傳效果最好。 |
headers | object | 選填的額外標頭,例如 {"X-Campaign": "spring"}。標準標頭(From、To、Subject、Reply-To、Message-ID、List-Unsubscribe 等)不能在此設定。 |
attachments | array | 選填。項目格式為 {"filename": "invoice.pdf", "content_base64": "...", "type": "application/pdf"}。最多 10 個檔案,總計 10 MB。 |
category | string | 選填。字母、數字、.、_、-,最多 64 個字元。 |
tags | array of strings | 選填。最多 10 個標籤供你自行篩選,每個最多 64 個字元。 |
track_opens, track_clicks | boolean | 選填。為這封郵件開啟或關閉開啟追蹤(追蹤像素)與點擊追蹤(連結改寫為經由 t.tm.yeapcloud.net)。僅適用於 HTML。省略時套用發信流的預設值。兩者預設皆關閉。 |
variables | object | 選填。替換 subject、html 與 text 中的 {{name}} 佔位符。在 html 中的值會經過 HTML 跳脫。未知的佔位符保持原樣。 |
單次呼叫在 to、cc 與 bcc 中合計最多 50 位收件人。每位收件人會收到一份獨立副本,各有自己的郵件 ID。若要對許多人寄送不同內容,請使用批次端點。
範例:
curl -X POST https://api.tm.yeapcloud.net/v1/send \
-H "Authorization: Bearer ytm_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"from": "Example <noreply@example.com>",
"to": ["alice@example.org"],
"subject": "Your order {{order_id}} has shipped",
"html": "<p>Hi {{name}}, your order is on its way.</p>",
"text": "Hi {{name}}, your order is on its way.",
"category": "shipping",
"tags": ["order"],
"variables": {"name": "Alice", "order_id": "A-1024"}
}'回應:
{
"message_ids": ["01JAQ3Z9V8K4M2N6P7R5S1T0XW"],
"rejected": []
}message_ids 列出已接受的副本。rejected 列出未寄出的收件人,每筆包含 rcpt 與 reason。即使部分甚至全部收件人被拒,呼叫仍可能回傳成功,所以務必檢查 rejected:
reason | 說明 |
|---|---|
invalid_recipient | 地址無效 |
suppressed:<scope>:<reason> | 地址在抑制名單中,例如 suppressed:global:hard_bounce |
sandbox_recipient_not_verified | 發信流處於沙箱中,且不允許寄給該收件人 |
rate_limited_hourly, rate_limited_sandbox_or_hourly | 每小時限額(或沙箱每日限額)已用完 |
monthly_quota_exceeded | 每月額度已用完且不允許超量 |
enqueue_failed | 郵件無法加入佇列,不會計入配額。請重試。 |
使用 requests 的 Python 範例:
import requests
resp = requests.post(
"https://api.tm.yeapcloud.net/v1/send",
headers={"Authorization": "Bearer ytm_your_api_key"},
json={
"from": "Example <noreply@example.com>",
"to": "alice@example.org",
"subject": "Welcome",
"html": "<p>Welcome aboard!</p>",
"text": "Welcome aboard!",
"category": "welcome",
},
timeout=30,
)
resp.raise_for_status()
result = resp.json()
print(result["message_ids"], result["rejected"])批次傳送:POST /v1/batch
一個請求最多傳送 500 封郵件。每個項目的欄位與 /v1/send 相同。
{
"messages": [
{"from": "noreply@example.com", "to": "a@example.org", "subject": "Hi A", "text": "Hello A"},
{"from": "noreply@example.com", "to": "b@example.org", "subject": "Hi B", "text": "Hello B"}
]
}回應中每封郵件對應一個結果,順序相同:
{
"results": [
{"index": 0, "message_ids": ["01JAQ..."], "rejected": []},
{"index": 1, "error": {"code": "invalid_request", "message": "subject is required (single line)"}, "rejected": []}
]
}查詢郵件:GET /v1/messages
| 查詢參數 | 說明 |
|---|---|
status | 依狀態篩選:queued、delivered、deferred、bounced、complained、rejected 或 failed |
to | 依收件人地址篩選 |
since | 只傳回在此時間或之後建立的郵件,例如 2026-10-01T00:00:00Z |
limit | 每頁筆數,1–100(預設 50) |
before | 取得下一頁時,傳入上一個回應中的 next_before 值 |
回應格式為 {"data": [...], "next_before": "..."}。最後一頁的 next_before 為 null。郵件保留 30 天。
GET /v1/messages/{id} 傳回單一郵件及其事件:
{
"id": "01JAQ3Z9V8K4M2N6P7R5S1T0XW",
"stream_id": 12,
"source": "api",
"to": "alice@example.org",
"from": "Example <noreply@example.com>",
"subject": "Welcome",
"category": "welcome",
"tags": [],
"status": "delivered",
"smtp_code": 250,
"smtp_response": "250 2.0.0 OK",
"opened_at": null,
"clicked_at": null,
"unsubscribed_at": null,
"created_at": "2026-10-20 10:15:02",
"updated_at": "2026-10-20 10:15:04",
"events": [
{"type": "delivered", "at": "2026-10-20 10:15:04", "rcpt": "alice@example.org", "smtp_code": 250, "mx": "mx.example.org"}
]
}抑制名單:/v1/suppressions
這些端點用於管理 API 金鑰所屬發信流的抑制名單。
| 請求 | 內文或查詢參數 | 回應 |
|---|---|---|
GET /v1/suppressions | 查詢參數 email(選填)、limit(1–500,預設 100)、offset | {"data": [{"id", "scope", "email", "reason", "category", "note", "created_at"}]} |
POST /v1/suppressions | {"email": "..."} 或 {"emails": [...]}(最多 1,000 個),選填 category | {"added": 2, "invalid": []} |
DELETE /v1/suppressions | {"email": "..."},選填 category(內文或查詢參數) | {"removed": 1} |
透過 API 新增的地址,原因為 manual。帳戶層級與全域抑制記錄無法用 API 變更。請參閱抑制名單與退訂。