发送邮件
使用 /v3/smtp/email 端点发送单封事务性邮件。
端点
POST /v3/smtp/email请求体
基础邮件
{ "sender": { "name": "Your App", }, "to": [ { "name": "John Doe" } ], "subject": "Welcome to our service!", "htmlContent": "<html><body><h1>Welcome!</h1><p>Thank you for signing up.</p></body></html>"}进阶邮件
{ "sender": { "name": "Your App", }, "to": [ { "name": "John Doe" } ], "cc": [ { "name": "CC User" } ], "bcc": [ { } ], "subject": "Order Confirmation #{{order_id}}", "htmlContent": "<html><body><h1>Order Confirmed</h1><p>Your order {{order_id}} has been confirmed.</p></body></html>", "textContent": "Your order {{order_id}} has been confirmed.", "params": { "order_id": "12345" }, "tags": ["transactional", "order-confirmation"], "headers": { "X-Custom-Header": "custom-value" }}代码示例
JavaScript/Node.js
const brevo = require('@getbrevo/brevo');
const apiInstance = new brevo.TransactionalEmailsApi();apiInstance.setApiKey(brevo.TransactionalEmailsApiApiKeys.apiKey, process.env.BREVO_API_KEY);
const sendEmail = async () => { const sendSmtpEmail = new brevo.SendSmtpEmail();
sendSmtpEmail.subject = "Welcome to our service!"; sendSmtpEmail.htmlContent = "<html><body><h1>Welcome!</h1></body></html>";
try { const result = await apiInstance.sendTransacEmail(sendSmtpEmail); console.log('Email sent:', result); return result; } catch (error) { console.error('Error sending email:', error); throw error; }};Python
import sib_api_v3_sdkfrom sib_api_v3_sdk.rest import ApiException
configuration = sib_api_v3_sdk.Configuration()configuration.api_key['api-key'] = 'YOUR_API_KEY'
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(sib_api_v3_sdk.ApiClient(configuration))
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail( subject="Welcome to our service!", html_content="<html><body><h1>Welcome!</h1></body></html>")
try: api_response = api_instance.send_transac_email(send_smtp_email) print(api_response)except ApiException as e: print("Exception when calling TransactionalEmailsApi->send_transac_email: %s\n" % e)cURL
curl -X POST "https://api.brevo.com/v3/smtp/email" \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -H "api-key: YOUR_API_KEY" \ -d '{ "sender": { "name": "Your App", "email": "[email protected]" }, "to": [ { "email": "[email protected]", "name": "John Doe" } ], "subject": "Welcome to our service!", "htmlContent": "<html><body><h1>Welcome!</h1></body></html>" }'响应
成功响应(201 Created)
{}错误响应
API 密钥无效(401)
{ "code": "unauthorized", "message": "Invalid API key provided"}邮箱格式无效(400)
{ "code": "invalid_parameter", "message": "Invalid email format in 'to' field"}超出速率限制(429)
{ "code": "too_many_requests", "message": "Rate limit exceeded"}参数
必填参数
| 参数 | 类型 | 说明 |
|---|---|---|
sender | object | 发件人信息 |
to | array | 收件人对象数组 |
subject | string | 邮件主题行 |
可选参数
| 参数 | 类型 | 说明 |
|---|---|---|
htmlContent | string | 邮件的 HTML 内容 |
textContent | string | 邮件的纯文本内容 |
cc | array | 抄送收件人 |
bcc | array | 密送收件人 |
replyTo | object | 回复地址 |
attachments | array | 文件附件 |
params | object | 模板参数 |
tags | array | 用于追踪的邮件标签 |
headers | object | 自定义请求头 |
模板变量
在主题和内容中使用模板变量:
{ "subject": "Welcome {{firstName}}!", "htmlContent": "<h1>Hello {{firstName}} {{lastName}}</h1>", "params": { "firstName": "John", "lastName": "Doe" }}附件
添加文件附件:
{ "attachments": [ { "content": "base64_encoded_content", "name": "invoice.pdf" }, { "url": "https://example.com/document.pdf", "name": "document.pdf" } ]}最佳实践
- 同时提供 HTML 和纯文本内容,可提升送达率
- 使用模板变量,不要拼接内容字符串
- 填写规范的发件人信息,避免被垃圾邮件过滤器拦截
- 添加标签,便于追踪和分析
- 发送前校验邮箱地址
- 优雅处理错误,配合重试逻辑
速率限制
- 免费账号:每天 300 封邮件
- 付费账号:取决于你的套餐
- 突发上限:每分钟 100 封邮件
追踪
通过该端点发送的邮件会自动追踪以下数据:
- 送达状态
- 打开率
- 点击追踪
- 退信处理
在 Brevo 仪表板的 Statistics → Transactional 中查看详细分析。
相关端点
- 邮件模板
- 附件
- 邮件统计