安装
pip install voaaro版本 0.4.0。需要 Python 3.9 或更高版本,无任何依赖(标准库),带类型标注(py.typed,已通过 mypy --strict 检查)。
配置
与 resend-python 一样,可以在模块级别配置:
import os
import voaaro
voaaro.api_key = os.environ["VOAARO_API_KEY"]
voaaro.base_url = "https://mail.example.com"也可以使用客户端(支持多个实例,超时可调,默认 30 秒):
client = voaaro.Voaaro(api_key=os.environ["VOAARO_API_KEY"], base_url="https://mail.example.com", timeout=30, max_retries=2)
client.emails.send({...})不传参数时,Voaaro() 会读取 VOAARO_API_KEY 和 VOAARO_BASE_URL。密钥绝不会出现在 repr(client) 或错误消息中。
发送
email = voaaro.Emails.send(
{
"from": "Acme <notifications@example.com>",
"to": ["client@example.org"],
"subject": "Welcome",
"html": "<p>Your account is ready.</p>",
"attachments": [{"filename": "guide.pdf", "content": pdf_bytes}], # bytes encoded by the SDK
},
{"idempotency_key": "welcome-42"},
)
print(email["id"])重试与幂等性
- 遇到网络错误或超时、
429 rate_limit_exceeded或 500、502、503、504 时,请求最多重试max_retries次(默认 2 次,可在客户端或voaaro.max_retries上设置);遇到daily_quota_exceeded或其他4xx错误时从不重试。等待时间:以Retry-After为准(最多 60 秒),否则为 0.5 秒 × 2^n 并加入随机抖动,最多 8 秒。 - 每个
POST请求都会带上Idempotency-Key:优先使用您提供的键(作为最后一个参数传入{"idempotency_key": …}),否则使用 SDK 生成的 UUID v4,每次尝试都相同。 max_retries=0:不重试,也不生成键。sleep=用于替换两次尝试之间的time.sleep(加快测试)。VoaaroError.retry_after给出服务要求的等待时间。
分页
每个基于游标的列表都有对应的生成器 …_all,采用惰性求值(每次一页):
for c in voaaro.Contacts.list_all({"segment_id": segment_id, "unsubscribed": False}):
print(c["email"])
ids = [s["id"] for s in client.suppressions.list_all({"reason": "bounce"})]| 列表 | 生成器 |
|---|---|
X.list(params) | X.list_all(params):Emails、Emails.Receiving、ApiKeys、Domains、Suppressions、Webhooks、Contacts、Contacts.Imports、Segments、Audiences、Topics、ContactProperties、Broadcasts、Forms、NewsletterTemplates、Automations |
X.list(id, params) | X.list_all(id, params):Contacts.Segments、Audiences.Contacts、Webhooks.deliveries |
Emails.events、Segments.list_contacts、Broadcasts.recipients、Forms.signups、Automations.runs | events_all、list_contacts_all、recipients_all、signups_all、runs_all |
每页 100 条(或指定的 limit);指定的 after 作为起点,不接受 before。
其他功能
voaaro.Batch.send([...], {"batch_validation": "permissive"})
voaaro.Emails.get(email_id)
voaaro.Emails.list({"limit": 20})
voaaro.Emails.update({"id": email_id, "scheduled_at": "in 2 hours"})
voaaro.Emails.cancel(email_id)
voaaro.Emails.events({"type": "bounced", "created_after": "2026-11-01T00:00:00Z"})
voaaro.Emails.Receiving.list({"limit": 10}) # personal key with receiving
voaaro.Domains.create({"name": "notify.example.com"})
voaaro.Suppressions.add({"email": "former.customer@example.org", "domain": "example.com"})
voaaro.ApiKeys.create({"name": "CI", "permission": "sending_access"})
voaaro.Contacts.create({"email": "ada@example.org", "first_name": "Ada"})
voaaro.Broadcasts.create({"segment_id": segment_id, "from": sender, "subject": subject, "html": html, "send": True})资源:Emails(含 Attachments 和 Receiving)、Batch、ApiKeys、Domains、Suppressions、Contacts、Segments、Audiences、Topics、ContactProperties、Broadcasts、Newsletter、Forms、NewsletterTemplates、Automations、Events、Webhooks、Organizations(运营方密钥 mko_…);在客户端上,名称相同但为小写(client.emails、client.api_keys、client.newsletter_templates 等)。
新闻通讯
voaaro.Newsletter.update_settings({"from": "Acme <news@example.com>", "postal_address": "Acme\n12 Lilac Street\nSpringfield"})
o = voaaro.Newsletter.overview({"days": 30})
f = voaaro.Forms.create({"name": "Website newsletter", "double_opt_in": True, "segment_ids": [segment_id]})
voaaro.Segments.preview({"filter": {"match": "all", "conditions": [{"field": "opened", "op": "none", "days": 90}]}})
voaaro.Broadcasts.links(broadcast_id)
voaaro.Broadcasts.choose_winner(broadcast_id, "b")
csv_text = voaaro.Broadcasts.export(broadcast_id)
t = voaaro.NewsletterTemplates.create({"name": "Monthly letter", "subject": "What's new", "html": html})
voaaro.Automations.create({"name": "After purchase", "trigger": {"type": "event", "event": "purchase"}, "steps": steps, "status": "active"})
voaaro.Events.send({"email": "ada@example.org", "name": "purchase", "data": {"total": 42}})
voaaro.Contacts.cleanup({"days": 180}) # simulation; "dry_run": False to act路由详情:新闻通讯。当密钥覆盖多个域名时,请指定 "domain"。
从您的应用进行订阅和退订(服务器端:Newsletter.subscribe、unsubscribe、get_subscription;浏览器端:PublicForm、solve_challenge),以及营销邮件定时发送(Broadcasts.schedule、unschedule、send_now、list_scheduled、check):请参见指南您自己的订阅表单、从您的应用退订和定时发送营销邮件,其中提供了每种 SDK 的示例。
联系人质量与送达率:验证地址但不保存(Contacts.verify_email)或重新验证联系人的地址(Contacts.reverify),按参与度和地址状态筛选(engagement、email_status),导出 CSV(Contacts.export),导入时排除无效地址(skip_invalid),以及营销邮件的送达率评分(Broadcasts.deliverability):请参见联系人、分组和主题和营销邮件,其中提供了每种 SDK 的示例。
错误
try:
voaaro.Emails.send(params)
except voaaro.RateLimitError as e:
print("later", e.retry_after)
except voaaro.ValidationError as e:
print("request to fix", e)
except voaaro.VoaaroError as e:
print(e)Webhook
event = voaaro.Webhooks.verify(request.get_data(), request.headers, os.environ["VOAARO_WEBHOOK_SECRET"])如果签名错误或已过期(5 分钟),会抛出 voaaro.WebhookVerificationError。多个签名密钥:传入列表 [新密钥, 旧密钥] 代替单个签名密钥(空签名密钥会被忽略);只要其中一个与请求头中的某个 v1 匹配,通知即被接受。完整示例和轮换:签名。
管理端点(详情):
w = voaaro.Webhooks.create({"endpoint": "https://app.example.com/webhooks/voaaro", "events": ["email.bounced"]})
w["signing_secret"] # shown only once
voaaro.Webhooks.rotate_secret(w["id"], {"grace_hours": 24})
for d in voaaro.Webhooks.deliveries.list_all(w["id"], {"status": "failed"}):
voaaro.Webhooks.deliveries.replay(w["id"], d["id"])