名称: brevo
版本: 1.0.0
描述: Brevo(原 Sendinblue)电子邮件营销 API,用于管理联系人、列表、发送事务性邮件和营销活动。适用于导入联系人、发送邮件、管理订阅或处理邮件自动化。
通过 Brevo 的 REST API 管理联系人、发送邮件并实现营销自动化。
BREVO_KEY=$(cat ~/.config/brevo/api_key)
所有请求都需要在请求头中包含:api-key: $BREVO_KEY
https://api.brevo.com/v3
| 操作 | 方法 | 端点 |
|---|---|---|
| 创建联系人 | POST | /contacts |
| 获取联系人 | GET | /contacts/{email} |
| 更新联系人 | PUT | /contacts/{email} |
| 删除联系人 | DELETE | /contacts/{email} |
| 列出联系人 | GET | /contacts?limit=50&offset=0 |
| 获取黑名单 | GET | /contacts?emailBlacklisted=true |
| 操作 | 方法 | 端点 |
|---|---|---|
| 获取所有列表 | GET | /contacts/lists |
| 创建列表 | POST | /contacts/lists |
| 获取列表中的联系人 | GET | /contacts/lists/{listId}/contacts |
| 添加到列表 | POST | /contacts/lists/{listId}/contacts/add |
| 从列表中移除 | POST | /contacts/lists/{listId}/contacts/remove |
| 操作 | 方法 | 端点 |
|---|---|---|
| 发送事务性邮件 | POST | /smtp/email |
| 发送营销活动 | POST | /emailCampaigns |
| 获取模板 | GET | /smtp/templates |
curl -X POST "https://api.brevo.com/v3/contacts" \
-H "api-key: $BREVO_KEY" \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"listIds": [10],
"updateEnabled": true,
"attributes": {
"NOMBRE": "John",
"APELLIDOS": "Doe"
}
}'
curl "https://api.brevo.com/v3/contacts/user@example.com" \
-H "api-key: $BREVO_KEY"
curl -X PUT "https://api.brevo.com/v3/contacts/user@example.com" \
-H "api-key: $BREVO_KEY" \
-H "Content-Type: application/json" \
-d '{
"listIds": [10, 15],
"attributes": {
"CUSTOM_FIELD": "value"
}
}'
curl -X POST "https://api.brevo.com/v3/smtp/email" \
-H "api-key: $BREVO_KEY" \
-H "Content-Type: application/json" \
-d '{
"sender": {"name": "My App", "email": "noreply@example.com"},
"to": [{"email": "user@example.com", "name": "John"}],
"subject": "欢迎!",
"htmlContent": "<p>你好 {{params.name}}</p>",
"params": {"name": "John"}
}'
curl -X POST "https://api.brevo.com/v3/smtp/email" \
-H "api-key: $BREVO_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": [{"email": "user@example.com"}],
"templateId": 34,
"params": {
"NOMBRE": "John",
"FECHA": "2026-02-01"
}
}'
curl "https://api.brevo.com/v3/contacts/lists?limit=50" \
-H "api-key: $BREVO_KEY"
curl -X POST "https://api.brevo.com/v3/contacts/lists/10/contacts/add" \
-H "api-key: $BREVO_KEY" \
-H "Content-Type: application/json" \
-d '{
"emails": ["user1@example.com", "user2@example.com"]
}'
导入联系人时,务必尊重退订状态:
import requests
BREVO_KEY = "your-api-key"
HEADERS = {'api-key': BREVO_KEY, 'Content-Type': 'application/json'}
BASE = 'https://api.brevo.com/v3'
def get_blacklisted():
"""获取所有已退订/黑名单邮箱"""
blacklisted = set()
offset = 0
while True:
r = requests.get(
f'{BASE}/contacts?limit=100&offset={offset}&emailBlacklisted=true',
headers=HEADERS
)
contacts = r.json().get('contacts', [])
if not contacts:
break
for c in contacts:
blacklisted.add(c['email'].lower())
offset += 100
return blacklisted
def safe_import(emails, list_id):
"""导入联系人,同时尊重退订状态"""
blacklisted = get_blacklisted()
for email in emails:
if email.lower() in blacklisted:
print(f"已跳过(已退订):{email}")
continue
r = requests.post(f'{BASE}/contacts', headers=HEADERS, json={
'email': email,
'listIds': [list_id],
'updateEnabled': True
})
if r.status_code in [200, 201, 204]:
print(f"已导入:{email}")
else:
print(f"错误:{email} - {r.text[:50]}")
Brevo 使用自定义属性存储联系人数据:
{
"attributes": {
"NOMBRE": "John",
"APELLIDOS": "Doe",
"FECHA_ALTA": "2026-01-15",
"PLAN": "premium",
"CUSTOM_FIELD": "any value"
}
}
在 Brevo 仪表板中创建属性:联系人 → 设置 → 联系人属性。
| 状态码 | 含义 |
|---|---|
| 200 | 成功(GET) |
| 201 | 已创建(POST) |
| 204 | 成功,无内容(PUT/DELETE) |
| 400 | 请求错误(检查负载) |
| 401 | API 密钥无效 |
| 404 | 联系人/资源未找到 |
updateEnabled: true:更新现有联系人而非失败Brevo 自动化可在以下情况下触发:
- 联系人被添加到列表
- 联系人属性更新
- 邮件被打开/点击
- 通过 API 发送的自定义事件
手动触发自动化:
curl -X POST "https://api.brevo.com/v3/contacts/import" \
-H "api-key: $BREVO_KEY" \
-H "Content-Type: application/json" \
-d '{
"listIds": [10],
"emailBlacklist": false,
"updateExistingContacts": true,
"emptyContactsAttributes": false,
"jsonBody": [
{"email": "user@example.com", "attributes": {"NOMBRE": "John"}}
]
}'
# 统计列表中的联系人数量
curl "https://api.brevo.com/v3/contacts/lists/10" -H "api-key: $BREVO_KEY" | jq '.totalSubscribers'
# 获取最近的联系人
curl "https://api.brevo.com/v3/contacts?limit=10&sort=desc" -H "api-key: $BREVO_KEY"
# 检查邮箱是否存在
curl "https://api.brevo.com/v3/contacts/user@example.com" -H "api-key: $BREVO_KEY"
# 获取账户信息
curl "https://api.brevo.com/v3/account" -H "api-key: $BREVO_KEY"